Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PouchDB + Conflict Resolution

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?

like image 738
dimko1 Avatar asked Sep 11 '26 15:09

dimko1


1 Answers

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:

  • The CouchDB docs on conflict resolution
  • The CouchDB wiki page on conflicts.
  • Understanding CouchDB Conflicts by Jan Lenhardt
like image 174
nlawson Avatar answered Sep 14 '26 05:09

nlawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!