Dexie.js: delete table from db

Created on 22 Jun 2016  路  14Comments  路  Source: dfahlander/Dexie.js

Hi,

I want to delete an object store from indexedDB but am not able to in the following code.

var db = new Dexie("FriendDatabase");
db.version(1).stores({
 friends: "++id,name,age"
});

db.friends.add({name: "Josephine", age: 21})
.then(function() {
   removeTable()
})
.catch(error);

function removeTable(){
  db.friends.delete('id')
  .then(function() {
        console.log('removed');
  })
  .catch(error);
}

function error(e) {
    console.log ("Error: " + (e.stack || e));
}

Can you please let me know how can I delete an object store with / without data?

Thank you.

question

Most helpful comment

Your last solution is right. It's just that there's a bug in dexie 2.x that has been resolved in 3.x. Using your last approach and upgrade dexie (npm install dexie@next) will resolve it

All 14 comments

No because that code tries to delete a single friend with the name 'id'.

To delete an object store you need to add a new version where you use null as the value of the stores spec. There's an example of this in the api reference page under quick reference.

db.version (1).stores ({
    friends: 'name, age'
});
db.version (2).stores ({
    friends: null
});

Notice you must also have version 1 kept there to make Dexie understand this is a deletion.

Thanks.

I see. I could delete the table on new db version, however it still persists in the previous db version.

Just a question: Can I transact using older version? If yes, can you provide a reference of how.

Thanks.

Explain 'still persist on previous db version '

I mean, if I do -> db.version(1).stores()._cfg.tables
I could see the deleted table and even under chrome inspector -> resources, its visible.

Also when I implemented this in my actual project, it throws 'DatabaseCloseError - Database has been closed.' Thats weird, as it works in a standalone html file.

The installed database will upgrade but there's no such thing as and old db version after the upgrade has taken place. Keeping the old version is just declarative so that it works on browsers which still has the old version.

If you need to read from the table you are going to delete, add an intermediate version with an upgrade() reading the data, then add an additional version where you set the stores to delete to null. This is not explained anywhere in the docs. Please reply if this solves the issue. Notice ie and edge will bug out when doing this. A workaround in Dexie for this is being planned.

Database closed error could happen if database has been explicitely deleted (or closed) prior to using it without explicitely calling db.open () before reading from it again

Sample on how to read from old version in an upgrade callback to move contents into another table :

db.version (1).stores ({
    friends: 'name, age'
});
db.version (2).stores ({
    contacts: 'name, age'
}).upgrade (() => {
    db.friends.each(friend => db.contacts.add(friend));
});
db.version (3).stores ({
    friends: null
});

Thank you. I finally got the table removed by upgrading version to db.verno+1.

However I need to find a way to get the latest version after I refresh html for adding new tables, which I will try to find.

Hi,

I have a similar problem, I have to move the data from table A to table B. I tried the example you provided above, but i can't get it working.

Here's my pen:
https://codepen.io/lukasvice/pen/WgqQwo?editors=0010

The database gets not updated to version 3 but remains on version 1, throwing a warning in the console.

If I remove version 3 (not removing the friends table) the data gets copied.

How can I delete the table after the copying is done?
Thank you!

This has been an issue for a long time but was fixed in Dexie 3.0.0-alpha.3.

I forked and modified your codepen sample and got it to work: https://codepen.io/dfahlander/pen/GXbZza. If you run it second time, you'll need to delete the db first.

The changes:

  1. Use [email protected]
  2. Use tx instead of db in the upgrade function.

Oh sorry, i didn't find the related issues. Thank you for your quick reply.

No probs! This issue didn't ever indicate it still was an issue before 3.0.0-alpha.3, but I'm glad it came up here for other readers ;)

I was just about to post this as a new issue, but then I took another look at this one and it seems to be a duplicate:


I am facing the following issue when using Dexie to define my databases:

Assume I made really stupid choice when intially naming my tables. Now, I found a way more suitable name for that table and would like to move its contents to this newly created table while getting rid of the old one in the next migration.

From reading the issues I found that using null in Version.stores will delete the database, so I tried doing the following:

db.version(1).stores({
  widgets: 'itemId,[itemId+timestamp]' // this is the "bad" name
})

db.version(2).stores({
  widgets: null,
  // this is the "good" name I want to migrate to
  items: 'itemId,[itemId+timestamp]'

}).upgrade(function (txn) {
  return txn.widgets.toArray(function (items) {
    return txn.items.bulkPut(items)
  })
})

This fails because apparently when upgrade is being called, the widgets database has already been deleted and its contents removed.

This had me come up with the following approach:

db.version(1).stores({
  widgets: 'itemId,[itemId+timestamp]'
})

db.version(2).stores({
  widgets: 'itemId,[itemId+timestamp]',
  items: 'itemId,[itemId+timestamp]'

}).upgrade(function (txn) {
  return txn.widgets.toArray(function (items) {
    return txn.items.bulkPut(items)
  })
})

db.version(3).stores({
  widgets: null,
  items: 'itemId,[itemId+timestamp]'
})

yet this fails for the same reason (there is no widgets database when running the upgrade handler).

I'm not entirely sure why that is:

  • Do I do something wrong in how I migrate the data in my upgrade function? Do I need to make sure this is blocking in some other way than just returning Promises?
  • Is this a limitation of IndexedDB or a possible issue in Dexie?
  • Is there another way of deleting the unwanted table other than re-defining it as null ?

Does this mean I will need to move to v3 at some point in order to get this functionality?

Your last solution is right. It's just that there's a bug in dexie 2.x that has been resolved in 3.x. Using your last approach and upgrade dexie (npm install dexie@next) will resolve it

Was this page helpful?
0 / 5 - 0 ratings