How to manage pouchdb and couchdb synchronization?
Asked Answered
S

1

7

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 here enter image description here

Sukhum answered 4/5, 2015 at 11:20 Comment(0)
H
6

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!

Handhold answered 4/5, 2015 at 12:16 Comment(2)
I am also aware of this approach and it is best but i have many documents in user database like transaction.so should i use separate document for each record? and how to manage multiple table data?can you explain me i am confused about it.Sukhum
You can have separate doc for each session-device combination – and each doc holds several transaction records that are later map-reduced to obtain final transaction state. This approach a) generates less docs (number of docs in DB impacts Pouch perfomance); b) generates less traffic; c) generates no conflicts.Underground

© 2022 - 2024 — McMap. All rights reserved.