PouchDB-手动管理冲突 [英] PouchDB - Manually managing conflicts

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

问题描述

是否可以管理来自客户端的同步冲突?

Is it possible to manage the sync conflicts from the client?

我的意思是,当pouchDB执行同步并检测到冲突时,是否有可能获得本地文档PouchDB正在尝试同步,并且CouchDB文档的最新版本是?如果我能同时获得两个文档,则可以将它们显示给用户,他可以选择保留哪个版本...

What I mean is, when pouchDB does a sync and detects a conflict, is it possible to get the local doc PouchDB is trying to sync and the last revision of CouchDB doc? If I can get both docs, I can display them to the user and he can choose which version to keep...

推荐答案

您很幸运,因为这正是CouchDB和PouchDB旨在解决的问题。

You're in luck, because this is exactly the problem CouchDB and PouchDB were designed to solve.

基本上,您可以阅读有关解决冲突的CouchDB文档。其中的所有内容也应适用于PouchDB。 (如果不是,那是一个错误。)。 CouchDB Wiki 的文字也不错。

Basically you can read up on the CouchDB docs on conflict resolution. Everything in there should also apply to PouchDB. (If it doesn't, it's a bug. ;)). The CouchDB wiki also has a nice writeup.

编辑:,以便提供更多详细信息,您需要使用?conflicts = true {conflicts:true} (在PouchDB中)。例如。您将获取如下文档:

so to provide more details, you'll want to fetch the document with ?conflicts=true ({conflicts:true} in PouchDB). E.g. you'll fetch a doc like this:

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

并像下面这样取回文档:

And get a doc back like this:

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

在这里,我从另一个数据库中引入了一个冲突,并且该数据库的修订已获胜(随机)。该文档的当前修订版本以2开头,而有冲突的版本也以2开头,表示它们都处于修订树的同一级别。

Here I have a conflict introduced from another database, and that database's revision has won (randomly). This document's current revision starts with 2-, and the conflicting version also starts with 2-, indicating that they're both at the same level of the revision tree.

到获得有冲突的版本,您只需获取有冲突的版本并调用:

To get the conflicting version, you just grab the conflicting rev and call:

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

您会得到:

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

这两个有冲突的版本给用户,然后您可以在这两个版本的基础上添加第三个修订版,该版本可以合并结果,也可以选择丢失的版本,也可以选择您想要的版本。下一个修订版将带有3-前缀。

So after presenting the two conflicting versions to the user, you can then add a 3rd revision on top of both of these, which either combines the results, or chooses the losing version, or whatever you want. This next revision will be prefixed with 3-. Make sense?

编辑:显然,您还需要删除有冲突的版本,否则它仍会显示在 _conflicts 。参见此答案

Apparently you also need to delete the conflicting version, or else it will still show up in _conflicts. See this answer.

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

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