Updating local DB table from Database with Dexie.Syncable

712 views
Skip to first unread message

Yasir Shaikh

unread,
Dec 8, 2015, 3:34:00 AM12/8/15
to Dexie.js
Dear David

I got the answer of my last Question for offset and limit , I have done some of operation with local DB using Dexie js.

I have done with Add Records and fetching and deleting existing record .

now I have one scenario .

In my MVC application , i want to check that which kinds of records have been updated on SQL server database and those updated records i want to fetch and store 
in local DB table .

means i have done with storing and fetching records in local DB but some one update few records in SQL server Database using from other login so then 
what kind of functionality dexie provide me .fetching updated records from server database and storing in local DB.

Tell me what can i do better solution . i have only one day for developing .

thanks 
Yasir Shaikh 


David Fahlander

unread,
Dec 8, 2015, 7:23:48 AM12/8/15
to Dexie.js
The question is more of a backend-related question. Personally I do that kind of sync between Dexie and a backend SQL server but I needed to implement the backend for it. I added a table for change-tracking at backend and each change (Insert/update/delete) to the db is tracked in that table.
At client side, I use the addon Dexie.Syncable.js that provides a framework for syncing between local DB and remote. Then to connect it with backend, I usa a client-side implementation of ISyncProtocol that connects to my server and sends local changes and retrieves remote changes.

This is not done in one single day though. You would need to think it through but you have a good starting point at the following sample implementation:

Yasir Shaikh

unread,
Dec 14, 2015, 11:30:17 PM12/14/15
to Dexie.js
Hi David,

As per your programming is best way but I have done with another way using web worker .

I have used a Web Worker create ajax post request pass data to server DB from localDB match Data with linq and finally get the updated Record 
through JSON format then Updating in local DB 

I have done like this , tell me this is good way or not , suggest me best way 

this is my Worker.js

var i = 0;
onmessage = function (e) {
    if (e.data[0] === "start") {
        done()

    }
    else {
        //console.log(e.data)
        debugger;
        
        doCall(e.data[0],e.data[1]);
       
        
    }
};



//sending information to server
function doCall(data,url) {
    try {
        var resourceData = JSON.stringify(data);
        var requestUrl = url;
        var client = null;
        if (XMLHttpRequest) {
            client = new XMLHttpRequest();
        } 
        else if (window.ActiveXObject) {
            client = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
        client.onreadystatechange = function () {
            if (client.readyState == 4 && client.status == 200) {
                var myArr = JSON.parse(client.responseText);
                callBackreturn(myArr);
            }
        };
        client.open("POST", requestUrl, true);
        client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        client.send('jsonData=' + resourceData)
    } catch (ex) {
        
    }
}

//waiting and processing server response
function callBackreturn(response) {
    try {
        postMessage(response);
        
    }
    catch (ex) {
       
    }
}

Index.chtml page

java script function 

setInterval(init, 50000)

function init() {
        debugger;
        var requestUrl = '@Url.Action("getRsourceUpdatedRecords")';

        if (typeof (Worker) === undefined) {
            alert('No webworker supported');
            return false;
        }

        myWorker = new Worker("../Scripts/worker.js");
        var resourcedata = fetchlocalResourceRecord();

        resourcedata.then(function (a) {
            
            myWorker.postMessage([a,requestUrl]);
        });

        myWorker.onmessage = function (e) {
            updateRecord(e.data);

        };

    }

function updateRecord(data)
    {
        debugger;
        db.transaction("rw", db.resourceTbl, function () {

            jQuery.each(data, function (index, value) {
                db.resourceTbl.where("ResourceId").equals(this.ResourceId).modify({ EmployeeCode: this.EmployeeCode, DisplayName: this.DisplayName 
                    , AdrenalinResourceStatus: this.AdrenalinResourceStatus, DateOfJoining: this.DateOfJoining
                })
            });

            //db.friends.where("shoeSize").aboveOrEqual(47).modify({isBigfoot: 1});
            
        }).catch(Dexie.ModifyError, function (e) {
                console.error(e.failures.length + " items failed to modify");

        }).catch(function (e) {
            console.error("Generic error: " + e);
        });

    }
Reply all
Reply to author
Forward
0 new messages