PouchDB Resolve Conflicts
PouchDB plugin to assist in PouchDB conflict resolving.
Disclaimer
Conflict resolution should better be done server side to avoid hard to debug
loops when multiple clients resolves conflicts on the same documents.
Installation
pouch-resolve-conflicts is hosted on npm.
Node
Install via npm install pouch-resolve-conflicts
var PouchDB = require('pouchdb')
PouchDB.plugin(require('pouch-resolve-conflicts'))
Browser
Use the browserified build.
<script src="pouchdb.js"></script>
<script src="pouch-resolve-conflicts.js"></script>
Usage
function resolveFun (a, b) {
if ('foo' in a && 'foo' in b) return
if ('foo' in a) return a
if ('foo' in b) return b
a.foo = 'bar'
return a
}
var db = new PouchDB('mydb')
db
.bulkDocs({
docs: [
{ _id: 'mydoc', _rev: '1-one', foo: 'bar' },
{ _id: 'mydoc', _rev: '1-two', bar: 'baz' }
],
new_edits: false
})
.then(function(response) {
return db.get('mydoc', { conflicts: true })
})
.then(function(doc) {
return db.resolveConflicts(doc, resolveFun)
})
function resolveFun (a, b) {
return new Promise(function(resolve, reject) {
if ('foo' in a && 'foo' in b) return resolve(undefined)
if ('foo' in a) return resolve(a)
if ('foo' in b) return resolve(b)
a.foo = 'bar'
return resolve(a)
})
}
Tests
npm test