3

I have a really simple question about hard topic:

How does conflict resolution work in PouchDB?

I looked at the documentation, as well as quickly googling, but it didn't help. So, how to do I handle conflict management in my application which is using PouchDB?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dimko1
  • 872
  • 7
  • 15

1 Answers1

7

Here's how you do it in CouchDB, which you can directly translate into PouchDB terms since the APIs are exactly the same.

You fetch a document, using conflicts=true to ask for conflicts (get() with {conflicts:true} in PouchDB):

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

You receive a doc like this:

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

There is a conflict introduced from another database, and that database's revision has (randomly) won. If you used bi-directional replication, both databases will provide this same answer.

Notice that both revisions start with "2-." This indicates that they are both the second revision to the document, and they both live at the same level of the revision tree.

Using the revision ID, you fetch the conflicting version (get() with {rev=...} in PouchDB:

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

You receive:

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

After presenting the two conflicting versions to the user, you can then PUT (put()) a third revision on top of both of these. Your third version can combine the results, choose the loser, or whatever you want.

Advanced reading:

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
nlawson
  • 11,510
  • 4
  • 40
  • 50
  • Thanks for the answer. But question is more - how to sync items when you have live replication On? – dimko1 Jul 02 '14 at 09:08
  • For a tutorial on conflict resolution during live replication see: http://stackoverflow.com/questions/18434686/pouchdb-how-to-resolve-conflicts-with-continuous-replication – Colin Skow Jul 20 '14 at 07:59