PouchDB + Conflict Resolution

强颜欢笑 提交于 2019-12-01 11:54:25

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/24450495/pouchdb-conflict-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!