underpouch
An underscore API for PouchDB with added bonuses!
npm install underpouch
usage
var PouchDB = var _pouch = var db = 'db'
Now you can query PouchDB with familiar _.underscore functions:
_pouch
(note that the following examples return docs with _id and _rev but are trimmed, unless otherwise indicated, for brevity)
Collections (for sifting, sorting docs)
find
_pouch.find(db, truthTest, callback)
Looks through each doc in the db, returning the first one that passes a truth test.
_pouch
filter
_pouch.filter(db, truthTest, callback)
Looks through each doc in the db, returning an array of docs that pass a truth test.
_pouch
where
_pouch.where(db, properties, callback)
Looks through each doc in the db, returning an array of all the docs that contain all of the key-value pairs listed in properties.
_pouch
findWhere
_pouch.findWhere(db, properties, callback)
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
_pouch
max
Returns the doc with the maximum value. If no iteratee function is provided, defaults to _.id (parseInt will be used).
_pouch
all !
_pouch.all(db, callback)
Returns an array of all the docs in the db
_pouchalldb { //allDocs = [{_id: 'all'},{_id: 'the'}, {_id: 'docs'}] }
deleteDocs !
_pouch.deleteDocs(db, callback)
Deletes all docs in database, without deleting the database (adds { _deleted : true } to each doc)
_pouch
Object Functions (for modifying docs)
extend
_pouch.extend(db, destinationDocId, sourceDoc, callback)
Copy all of the properties in the source doc over to the destination doc, put and return the destination doc with its updated rev. It's in-order, so the last source will override properties of the same name in previous arguments.
_pouch
extendPut !
_pouch.extendPut(db, destinationDocId, sourceDoc, callback)
Like _pouch.extend but put()
's the sourceDoc even if destination doc is not existing.
_pouch
extendPutOrPost !
_pouch.extendPutOrPost(db, destinationDoc, sourceDoc, callback)
Like _pouch.extendPut but will post() the doc if no id is provided.
_pouch
merge ! (lodash)
_pouch.merge(db, destinationDocId, sourceDoc, callback)
Like _pouch.extend but uses lodash's merge so that child properties are merged, not overwritten. Ie: _pouch.extend will overwrite properties whereas _pouch.merge will merge them.
//originalDoc = { _.id: 'food', fruits: ['mango', 'lemon']} _pouch
mergePutOrPost !
_pouch.merge(db, destinationDocId, sourceDoc, callback)
Like _pouch.mergePutOrPost but will post (if no _id) or put the doc if not already existing.
_pouch
replace !
_pouch.replace(db, doc, callback)
Overwrites an existing doc, regardless of revision (shortcut for doing db.get and then db.put) or if doc existing or not (posts if not existing).
//no doc existing: _p