Beginning with Dexie

1,032 views
Skip to first unread message

bdu...@gmail.com

unread,
Jul 9, 2015, 7:52:23 PM7/9/15
to dex...@googlegroups.com
Hello. I am a new user with Dexie (just began today). I am having some early frustrations with it as it seems like an amazing library, but I feel like I am missing something important as nothing I am trying is working.

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!

David Fahlander

unread,
Jul 10, 2015, 6:34:24 AM7/10/15
to bdu...@gmail.com, dex...@googlegroups.com

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.

bdu...@gmail.com

unread,
Jul 10, 2015, 10:33:57 AM7/10/15
to dex...@googlegroups.com, da...@awarica.com
Thank you very much for your reply. After re-reading my post, I realize it was a bit all over the place since I had tried a bunch of things to get it to work without any success. The code I posted was just a simple example I built to try out Dexie without anything else. I inspected the code this morning and realized that my then function wasn't firing because the db didn't exist yet so it was looking for the error function which I had not provided. So, maybe looking at my actual issue is a better idea.

What I have is a JSON object of tables, which I loop through to get a JSON object of fields and data for each table. I then need to create the tables if needed, and if there have been no changes (the audit table is empty), clear out any existing data and then insert the data.

Thus far, I have been unsuccessful in even properly creating the tables. While I can pick a version number, it does not work to update the db. I first tested this with version 1 without any tables to see if it would work. It did create the db, but now refuses to add any of my needed tables. So, I tried incrementing the version to 2, yet it still does not work.

So, I guess my first question is, is there any way to either add tables after specifying the version, or easily update the tables knowing only the current JSON list of tables?

I have other questions, but I might be able to figure them out once I get past this initial hurdle. Not sure if code will help, but here is some to help illustrate what I need:
function getDB(dbName, tableJSON) {
db = new Dexie(dbName);
//somehow compare tableJSON to existing db to see if updates are needed
//if needed, do the updates
//if not or once done with the above, check specific tables for row count
}

Thanks!

David Fahlander

unread,
Jul 10, 2015, 12:25:55 PM7/10/15
to Brian Duffey, dex...@googlegroups.com

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.

bdu...@gmail.com

unread,
Jul 10, 2015, 12:47:08 PM7/10/15
to dex...@googlegroups.com, da...@awarica.com
OK, I do see that its not easy to have dynamic databases, however that is what I have been tasked with so here I am. In terms of the upgrade, it seems completely random to me when it decides to upgrade or not.

Here is an example I put up publicly as I cannot see why sometimes it works and sometimes it does not:
http://jsfiddle.net/duffmaster33/vn2b2zLv/2/

In this example, I have defined 4 different schemas. Prior to running it, set currTables to tables1, which should then create the db for tables1. Then, update to tables 2, and you should see what I see, is that even though I am specifying the old and new stores, it does not actually upgrade the db. Am I doing something wrong here? I saw another post you made that says to upgrade you can close then open the database, which is what I do, to no avail. What is most weird to me is that one out of like 10-20 times, the upgrade will actually go through. However, when that does occur, about half the time only the version number of the browser db goes up and the structure does not change.

bdu...@gmail.com

unread,
Jul 10, 2015, 1:38:16 PM7/10/15
to dex...@googlegroups.com, da...@awarica.com, bdu...@gmail.com
OK, just found the generic error catcher so I added that here:
http://jsfiddle.net/duffmaster33/vn2b2zLv/3/

What it says is "Error: Database version changed by other database connection." I'm not sure what this means since wouldn't my browser have to do the changing? Since I only have this open in one tab, I don't understand what its saying, especially since now the version number keeps going up but no change is ever made??

David Fahlander

unread,
Jul 10, 2015, 4:14:24 PM7/10/15
to Brian Duffey, dex...@googlegroups.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

David Fahlander

unread,
Jul 10, 2015, 4:29:59 PM7/10/15
to Brian Duffey, dex...@googlegroups.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

Message has been deleted
Message has been deleted

bdu...@gmail.com

unread,
Jul 10, 2015, 4:39:55 PM7/10/15
to dex...@googlegroups.com, da...@awarica.com
Thanks, I appreciate any help you can give. Currently, I am only testing in Chrome, so no IE. I am also directly pulling from github, so should have the latest branch.

Your comment about other connections made me try moving my db.close() a little higher, which actually prevented the error message. Did not help the actual update though, albeit now with no errors I can see.

When you do have time to look into, one other thing I do not understand is in my fiddle, I have to set db to a Dexie database on both lines 11 and 73. If I skip line 11, the db will not even initially create. This doesn't seem right to me, so perhaps this is where I am going wrong, or if not, maybe you can help me understand why it is needed. Thanks again!

bdu...@gmail.com

unread,
Jul 13, 2015, 12:51:29 PM7/13/15
to dex...@googlegroups.com, da...@awarica.com
Hi there, just sending a reminder to take a look at my fiddle when you have a chance, per your request. Thanks!

David Fahlander

unread,
Jul 14, 2015, 3:10:28 AM7/14/15
to dex...@googlegroups.com, bdu...@gmail.com, da...@awarica.com, bdu...@gmail.com
Hi,

I had not that much time to look at it, but here's the things that I found: You should change the link to Dexie.js so that it reference source version (src/Dexie.js) instead of dist/latest just because dist/latest is only the latest released version and not the latest dev version and there might be issues fixed in it that could apply here.

I don't think the latest Dexie is the issue though. When I ran the code in Opera (which is basically a Chrome), it behaved as I would expect it to:
  • First time: Database was created
  • Second and all other times: successfully opened version 1.

I found some issues with the code. If you wanted it to migrade to version 2, 3, etc I think that never happens because each time the code runs, it will run in a new window and dbVersion will init to 0. Maybe you would need to unless you persist your local "dbVersion" var somewhere. Otherwise dbVersion++ will always be 1 on every run.

Also, createDB() function: Why does it create a new Dexie and then close it, and then define it's version. This is not how it is supposed to work. Change to closing the db before creating a new one:

    if (db) db.close();
    db
= new Dexie(...);

I must also admit that I just don't have the opportunity now to dive into your sample now during the rest of July because I'm on vacation and will be away from my computer 98% of my time. I'm so sorry. Maybe there's somebody else with Dexie experience that would want to help you with this.

Regards,
David
Reply all
Reply to author
Forward
0 new messages