PouchDB +冲突解决方案 [英] PouchDB + Conflict Resolution

查看:88
本文介绍了PouchDB +冲突解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于硬主题的非常简单的问题:

I have a really simple question about hard topic:

PouchDB中的冲突解决方案如何工作?

How does conflict resolution work in PouchDB?

我查看了文档,以及快速谷歌搜索,但它没有帮助。那么,如何在我的应用程序中处理使用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?

推荐答案

以下是在CouchDB中执行此操作的方法,你可以直接翻译成PouchDB术语,因为API完全相同。

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

你使用 conflicts = true 在PouchDB中询问与 {conflicts:true} 的冲突( get()):

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

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

您收到这样的文件:

{
"_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.

请注意,两个版本都以2-开头。这表明它们都是文档的第二个修订版,它们都位于修订树的同一级别。

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.

使用修订版ID,您可以获取冲突PouchDB中带有 {rev = ...} 的版本( get()

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

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

您会收到:

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

在向用户展示两个冲突的版本后,您可以 PUT put())在这两个版本之上的第三个版本。你的第三个版本可以结合结果,选择输家,或任何你想要的。

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.

高级阅读:

  • The CouchDB docs on conflict resolution
  • The CouchDB wiki page on conflicts.
  • Understanding CouchDB Conflicts by Jan Lenhardt

这篇关于PouchDB +冲突解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆