Git Product home page Git Product logo

pouch-resolve-conflicts's Introduction

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

pouch-resolve-conflicts's People

Contributors

craftzdog avatar dependabot[bot] avatar gnowoel avatar jo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pouch-resolve-conflicts's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.