pouchdb-ensure

1.0.0 • Public • Published

pouchdb-ensure

A PouchDB plugin.

Creates a doc or updates it, but only when necessary.

Good to make sure a design doc is saved on the database at startup time, and that it is updated on the database whenever it is changed on the code.

Usage

npm install --save pouchdb-ensure
PouchDB.plugin(require('pouchdb-ensure'))
 
var db = new PouchDB('mydb')
 
db.ensure({
  _id: 'a doc',
  value: 'a value'
})
  .then(doc => {
    // doc is saved and returned
    // doc === {_id: 'a doc', value: 'a value', _rev: '1-...'}
 
    return db.ensure({_id: 'a doc', value: 'a value'})
  })
  .then(doc => {
    // doc is just returned, not saved again
    // doc === {_id: 'a doc', value: 'a value', _rev: '1-...'}
 
    return db.ensure({_id: 'a doc', value: 'other value'})
  })
  .then(doc => {
    // doc is updated, now _rev is 2-... and value is 'other value'
  })

Dependencies (1)

Dev Dependencies (4)

Package Sidebar

Install

npm i pouchdb-ensure

Weekly Downloads

1

Version

1.0.0

License

Apache-2.0

Last publish

Collaborators

  • fiatjaf