PouchDB - Manually Conflict Management

Can sync conflicts with a client be managed?

What do I mean when pouchDB does sync and detects a conflict, is it possible for PouchDB's local doc to try to sync and the latest version of CouchDB doc? If I can get both documents, I can display them to the user and he can choose which version to keep ...

+2


source to share


1 answer


You are in luck, because this is exactly the problem that CouchDB and PouchDB problems were solved with.

Basically you can read the CouchDB Docs on Conflict Resolution . Anything in there should also apply to PouchDB. (If it isn't, that's a bug.;)). The CouchDB wiki also has a good entry.

Edit: so for more details, you'll want to check out the document using ?conflicts=true

( {conflicts:true}

in PouchDB). For example. you will get a document like this:

http://localhost:5984/db1/foo?conflicts=true

      

And return the document like so:

{
  "_id":"foo",
  "_rev":"2-f3d4c66dcd7596419c76b2498b3ba21f",
  "notgonnawork":"this is from the second db",
  "_conflicts":["2-c1592ce7b31cc26e91d2f2029c57e621"]
}

      

Here I have a conflict injected from another database and this version of the database won (by accident). The current version of this document starts with 2- and the conflicting version also starts with 2-, indicating that they are both at the same level in the version tree.



To get the conflicting revision, you simply grab the conflicting rev and call:

http://localhost:5984/db1/foo?rev=2-c1592ce7b31cc26e91d2f2029c57e621

      

And you get:

{
  "_id":"foo",
  "_rev":"2-c1592ce7b31cc26e91d2f2029c57e621",
  "notgonnawork":"this is from the first database"
}

      

So, after presenting the two conflicting versions to the user, you can add a third revision on top of both of them, which either merges the results, or chooses the playing version, or whatever. This next revision will be prefixed with 3-. It makes sense?

Edit: Obviously you also need to remove the conflicting version, otherwise it will show up in _conflicts

. See this answer .

+6


source







All Articles