Syncing IndexedDB with Sql Server
In addition to Editor and Kristof answers, I would like to add few more things since I've worked on a similar solution.
- First of as Editor said
IndexedDB
is a document-oriented database whileSQL
is relational database which means that the you should convert your data to a object model before storing them locally - Avoid using multiple tables in
IndexedDB
and trying to create joins. - Additional tables in
IndexedDB
should be used if you store different objects in the database or you have additional data for your main object (example list of articles in one table - article text in the second table).
Next thing is data synchronization - this can get really complicated if you update the data on both sides (client & server). The main problem that you'll be facing in the data synchronization is the fact that you'll have one main SQL
database and a lot of local client databases.
Here are couple things you should also consider:
- You'll need to pick when to do the synchronization timed or by client request.
- One other question that pops up is: What if the user works a lot of the time in the offline mode and updates an object which has been previously deleted by another user and that's stored to the
SQL
database. - And of course when you sync the data, split it into chunks, this will reduce the size of each response and
IndexedDB
works faster with smaller transactions. - I currently sync 500 items at a time (adjust this to the size of your objects)
- Also in
Chrome
you can accessIndexedDB
fromweb-workers
and make parallel requests - this can really speed things up.
This are just some facts you'll need to consider before you start implementing the solution, hope it helps.
SQL is relational database. IndexedDB is an indexed key/value store similar to a document-oriented database. There's no out of the box way to sync data from a breed of SQL to IndexedDB. You'll have to write that code manually (perhaps using a Polyfill like this one as an example), but note that inserting large amounts of data consecutively is not a good idea.
From what I understand in your question, you are just using indexeddb like a cache layer in case the db goes done. What I would do is the following.
In case you are working online a save of your data should exist out af 2 actions. Saving to your service (SQL server) and save to your indexeddb.
In case when you are offline I would do 2 operation. On is still updating your indexeddb, but I would add an extra object store where you save all of your operation that have to be preformed on your service. Once back online, you can processes them one by one.