Socket
Socket
Sign inDemoInstall

pouch-resolve-conflicts

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    pouch-resolve-conflicts

Assist in CouchDB conflict resolving.


Version published
Weekly downloads
40
decreased by-72.79%
Maintainers
1
Install size
13.6 kB
Created
Weekly downloads
 

Readme

Source

PouchDB Resolve Conflicts

PouchDB plugin to assist in PouchDB conflict resolving.

Build Status

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

// The resolve function. This function takes two documents and returns either
// one of them, a changed version of one of them, or nothing. In the latter the
// conflict will not be resolved. If there are more than two conflicting
// versions this function will be called with each version against the former
// result.
function resolveFun (a, b) {
  // cannot merge: return nothing
  if ('foo' in a && 'foo' in b) return
  
  // return one of the docs
  if ('foo' in a) return a
  if ('foo' in b) return b
  
  // return changed doc
  a.foo = 'bar'
  return a
}

var db = new PouchDB('mydb')

db
  // Lets have a conflict
  .bulkDocs({
    docs: [
      { _id: 'mydoc', _rev: '1-one', foo: 'bar' },
      { _id: 'mydoc', _rev: '1-two', bar: 'baz' }
    ],
    new_edits: false
  })
  // Query doc with `conflicts: true`
  .then(function(response) {
    return db.get('mydoc', { conflicts: true })
  })
  // And resolve it
  .then(function(doc) {
    return db.resolveConflicts(doc, resolveFun)
  })

// `resolveFun` can also return a promise:
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

Keywords

FAQs

Last updated on 12 Mar 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc