Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage pouchdb and couchdb synchronization?

Best approach to store multiple user data is per user per database. I am using this same approach.

I have couchdb on server and pouchdb for mobile application. I am maintaining each user data by creating separate database for the user in pouchdb and couchdb. That.That means i have multiple database in couchdb and one database in pouchdb.

usually in sqlbase database user data is store in different different table.

so in nosql pouchdb i am creating document for each table.

Actual problem i am facing is:

I have one document in each database that stores the transactions of user.

Client transaction is stored in pouchdb when he/she is offline and when application get on-line transaction sync to couchdb user database in to transaction document.

data is stored in transaction document is as follows

{
  "_id":"transaction ",
  "_rev":"1-3e5e140d50bf6a4d873f0c0f3e3deb8c",
  "data":[
    {
      "transaction_id":"tran_1",
      "transaction_name":"approve item",
      "status":"Pending",
      "ResultMsg":""
    },
    {
      "transaction_id":"tran_2",
      "transaction_name":"approve item",
      "status":"Pending",
      "ResultMsg":""
    }]
}

All these transaction is performed on server side and result is updated in these document.when ever any new transaction performed i store it in transaction document in data attribute.

Now i have 1 transaction in pouch and couchdb both means both are in sync.

Now when mobile application is offline it perform offline transaction that is stored in pouchdb transaction doc.

and on server side that 1 transaction is updated to success.

Now when application goes to on-line and sync perform i am losing my server side changes and finally data in transaction doc is as client pouchdb.

here i am losing server side data. so what is good approach or how can i solve it.

enter image description hereenter image description here

like image 497
Bhavesh Jariwala Avatar asked May 04 '15 11:05

Bhavesh Jariwala


People also ask

Why would you use PouchDB along with CouchDB?

CouchDB works well with modern web and mobile apps. PouchDB (local/client) enables applications to store data locally while offline, then synchronize it with CouchDB and compatible servers when the application is back online, keeping the user's data in sync no matter where they next login.

Where is PouchDB data stored?

PouchDB works offline as well as online with the same efficiency. It works offline by storing the data locally and synchronizing it to the servers and CouchDB when online. It stores data locally using IndexedDB and WebSQL in the browser.


1 Answers

What's happening is that you have conflicts to the same document, because it is modified in one way by the server and another way by the client. One conflicting version is winning arbitrarily, and the other is losing.

You can either resolve the conflicts or (the more reasonable solution in your case) store multiple documents per user instead of one big document.

Just because you have one database per user doesn't mean you need to have one document per user. :) E.g your docs could be:

{_id: "Tran_1", status: "Pending"}
{_id: "Tran_2", status: "Pending"}
// etc.

These documents would be created once on the client and updated once on the server. No possibility of conflicts. Piece of cake!

like image 195
nlawson Avatar answered Oct 22 '22 09:10

nlawson