Syncing IndexedDB with Sql Server

I am using standalone database communication on my page using HTML5 IndexedDB concepts. But first, I want to get big data for storage in my IndexedDb, so for this purpose I am trying to synchronize Sql server data with my indexed Datatbase index, but I did not find a solution for this. Is there any way to do this, if that helps me so ... thanks.

+3
source share
4 answers

From what I understand in your question, you just use indexeddb as a cache layer if db goes. I would do the following.

In case you work on the Internet, your data must exist outside of 2 actions. Saving your service (SQL server) and saving to your indexed drive.

If you are offline, I would do 2 operations. On is still updating your indexeddb, but I would add an additional repository of objects in which you save all of your operation, which must be preformed on your service. After going online you can process them one by one.

+2
source

In addition to the Editor and Kristof answers, I would like to add a few more things that worked on a similar solution.

  • First, from the editor, he said that IndexedDB is a document-oriented database, and SQL is a relational database, which means that you must convert your data to an object model before saving 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 (an example of a list of articles in one table is the text of the article in the second table).

Next: data synchronization - it can become very difficult if you update the data on both sides (client and server). The main problem that you will have to face in data synchronization is that you will have one main SQL and many local client databases.

Here are a few things you should also consider:

  • You will need to choose when to synchronize by time or at the request of the client.
  • Another question that appears: What if the user works offline for a long time and updates an object that was previously deleted by another user and which is stored in the SQL .
  • And of course, when you synchronize the data, divide it into pieces, this will reduce the size of each response, and IndexedDB works faster with smaller transactions.
  • I am currently syncing 500 items at a time (adjust this to fit your objects)
  • Also in Chrome you can access IndexedDB from web-workers and execute concurrent queries - this can really speed up the process.

These are just some of the facts that you will need to consider before proceeding with the implementation of the solution, I hope this helps.

+7
source

SQL is a relational database. IndexedDB is an indexed key / value store, similar to a documented database. There is no way out of the box to synchronize data from SQL to IndexedDB. You will need to write this code manually (possibly using a Polyfill as this as an example), but note that inserting large amounts of data is not a good idea .

+1
source

We have implemented crud operation in HTML5 and IndexedDB database to provide offline support. The next step will be the synchronization of offline and online databases. In this article, we are going to implement IndexedDB and SQL Server database synchronization using ASP.NET web API.

To synchronize a local database with a server database:

 $('#btnSyncLocal').click(function () { $.ajax({ url: 'api/service?revision=' + localStorage.customerRevision, type: 'GET', dataType: 'json', success: function (data) { if (data.Revision == localStorage.customerRevision) { alert('You are already working on the latest version.'); } else { syncData(data); } } }); }); 

Server database synchronization with local database:

 $('#btnSyncServer').click(function () { var customers = []; db.linq.from(config.objectStoreName).select().then(function () { if (customers.length > 0) { var postData = { revision: parseInt(localStorage.customerRevision, 10), appID: config.appID, customers: customers }; $.ajax({ url: 'api/service', type: 'POST', dataType: 'json', contentType: "application/json", data: JSON.stringify(postData), success: function (data) { if (data.Revision == localStorage.customerRevision) { alert('There is newer version on the server. Please Sync from server first.'); } else { syncData(data); } } }); } else { alert('There is no change in data after your last synchronization.'); } }, handleError, function (data) { if (data.Revision == -1) { customers.push(data); } }); }); 

For a complete implementation of this example, refer to this link:

Implementation example: Autonomous database synchronization (HTML5 IndexedDB) with an online database using Asp.Net web API

+1
source

Source: https://habr.com/ru/post/985860/


All Articles