The background is I am trying to create a generic offline app library that can be dropped into place for our clients. I have successfully done things like save the files, JS, CSS, etc., however for the data, I am trying to use Dexie and have thus far been unsuccessful.
Ultimately, what I need to happen is that a JSON list of tables needed will be passed to my function. I need to open Dexie (using var db = new Dexie('myDB')) and see if the tables already exist, or at the very least if some of the tables have records. Right now, I am successfully building my stores, however the IndexedDB database never updates as it stays at version 1 regardless of what I set my version to in Dexie. If I just open the database right away, I can see the tables in there. I can also just delete them right away and then create it fresh, but that isn't what I need.
I'm hopeful if I am given a tiny bit of guidance, I can finish the rest, so perhaps the following code will help illustrate my current issue:
var db = new Dexie('test'),
tables = { 'table1':'id++, field1, field2, field3', 'table2':'id++, field1, field2' };
db.delete();
db.open().then(function() {
if (db.tables.length === 0) {
//db.delete();
db.version(1).stores(tables);
db.open();
console.log('created new db');
}
console.log(db);
});
Right now, nothing in my promise is ever called, so while the db is deleted, it is never recreated. Once this is figured out, I need to be able to see if the stores exist, and if not, only then load them in. Thanks for any assistance ahead of time!
I'll try to give a longer answer later, since I'm short of time right now. But the short answer is: Dexie does those checks for you. Always define version(x).stores(). Don't provide them when database is open. Provide them before. After is to late. Only if you close and increment version you can add new tables to the database.
Best wishes
David
--
You received this message because you are subscribed to the Google Groups "Dexie.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dexiejs+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/dexiejs.
To view this discussion on the web visit https://groups.google.com/d/msgid/dexiejs/8e85986e-d95e-4281-83ec-1428995bbf90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
First, indexedDB is not very well designed for dynamically upgrading the database, even though it is possible, a forced upgrade would block of there were any open db connection on same origin in current window or another window. This can be handled if all db instances would also react on the onupgradeneeded event and close its Dexie instance and reload it's app.
Also, Dexie is designed for having a static database schema, not dynamic. You can force an upgrade but you'd need to specify not only the schema of the desired version but also the schema that is currently installed. Dexie will diff the two and create missing tables. If not specifying current schema, it will believe all tables must be created and it will fail to create those that already exist.
It is possible to open Dexie without version () in order to read the currently installed shema.
To view this discussion on the web visit https://groups.google.com/d/msgid/dexiejs/86e2447e-a6a1-47d6-93ee-93f4505bddd6%40googlegroups.com.
I'll see if I can have a look at this next week. Please remind me. Spontaneous reply: there might be several db connections in same window. Each open Dexie instance is a db connection and has to be closed when not in use anymore. Good that you have a jsfiddle so I'll be able to debug. Sorry for not being able to do that now. Kind regards David
To view this discussion on the web visit https://groups.google.com/d/msgid/dexiejs/d9bcf4ff-2294-40a8-8b22-ceed998cf7b5%40googlegroups.com.
A few more hints :
* IE has a race condition bug when upgrading DBs. Are you using IE?
* Use latest Dexie source from github src/Dexie.js in case a resolved issue that is not yet released has been fixed that resolves the issue you have
if (db) db.close();
db = new Dexie(...);