Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to change table name? #713

Closed
YvanY opened this issue Jun 1, 2018 · 2 comments
Closed

how to change table name? #713

YvanY opened this issue Jun 1, 2018 · 2 comments

Comments

@YvanY
Copy link

YvanY commented Jun 1, 2018

const db = new Dexie('mydb');
db.version(1).stores({
  friends: '++id, name, age'
});
db.version(2).stores({
  friends2: 'id, name, age'
}).upgrade(()=>{
  return db.friends.toArray().then(objs => {
    return db.friends2.bulkAdd(objs);
  });
});
db.version(3).stores({
  friends: null
});

I tried this, but it doesn't work, with an error message following:

TypeError Cannot read property 'toArray' of undefined

@dfahlander
Copy link
Collaborator

This is related to #105. It's still a bug.

Looking into it. I'll see what I could do...

@dfahlander
Copy link
Collaborator

This has been a headache for a long time (#88, #105 and maybe some other open issues), but I just submitted a solution that can resolve it. There were more visions for this, that is not taken into account. I really only needed to fix this particular lack of possibility to remove tables while still having access to them in the upgraders.

@YvanYeo Once my PR has passed all integration tests and published to npm (probably version 3.0.0-alpha.3), you can install it using

npm i dexie@latest

Then your upgrader code will need to use the transaction instance instead of db:

const db = new Dexie('mydb');
db.version(1).stores({
  friends: '++id, name, age'
});
db.version(2).stores({
  friends2: 'id, name, age'
}).upgrade(tx =>{
  return tx.friends.toArray().then(objs => {
    return tx.friends2.bulkAdd(objs);
  });
});
db.version(3).stores({
  friends: null
});

There is a test that verifies this issue is resolved. You can also look at that code sample in https://github.com/dfahlander/Dexie.js/blob/issue713/test/tests-upgrading.js#L515

With this PR, it will also be possible to change primary key, as requested in #88 even though a bit verbosely. And the code will not work on Internet Explorer 10 or 11. https://github.com/dfahlander/Dexie.js/blob/issue713/test/tests-upgrading.js#L550

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants