pouchdb-design v1.2.5
PouchDB Design Document Helpers
A PouchDB plugin for easier handling of CouchDB design documents.
Install
Install via NPM:
npm i pouchdb-design -SAnd use like any normal PouchDB plugin:
const pouchdesign = require("pouchdb-design");
PouchDB.plugin(pouchdesign);Usage
This plugin revolves around a class for managing a single design document. Instances of the class have several methods for fetching and saving, modifying design document properties, and adding custom functions and commonjs modules.
The typical flow is to fetch the existing design document, make some changes, and then save it back to database.
const design = db.design("mydesign");
design.fetch().then(() => {
design.view("allDocs", function(doc) {
emit(doc._id, { rev: doc._rev });
});
return design.save();
}).catch(e => {
console.error(e);
});API
db.design()
db.design( [ docOrId ] ) → DesignThis plugin attaches the method .design() to PouchDB instances. When called, it will return a Design object. It takes a single argument, which is an existing design document or a string id. If the id does not start with _design/, it is added automatically.
const design = db.design("mydesigndoc");const design = db.design("_design/mydesigndoc");const design = db.design({ _id: "_design/mydesigndoc" });design.fetch()
design.fetch() → Promise<this>This method will retrieve the existing design document from the database. Once complete, the existing design document is set internally for use by other synchronous methods.
If the design document does not exist, this method will continue successfully and set a blank, new document internally.
design.save()
design.save() → Promise<this>This will save the internal design document back to the database. This works like an upsert, where it will be created if it doesn't exist, or overwrites the existing.
design.toJSON()
design.toJSON() → objectReturns the CouchDB design document that the instances represents
design.clone()
design.clone() → designCopyReturns an exact duplicate of this design object.
design.id
Gets and sets the design's id. When setting, you can leave off the _design/ and it will be automatically added.
design.id = "mydesign";
console.log(design.id); // _design/mydesigndesign.get()
design.get( key ) → mixedReturns the exact value at key in the design document. This supports dot separated keys for deep values.
design.set()
design.set( key, value ) → thisSets value to key in the design document. Like .get(), key supports dot separated syntax for deep values. The value is normalized to be compatible with JSON and CouchDB. Functions, for example, are converted into strings.
design.unset()
design.unset( key ) → thisRemoves the value at key in the design document. Like .get(), key supports dot separated syntax for removing deep values.
design.view()
design.view( name, map [, reduce ] ) → this
design.view( name, mapReduceObj ) → this
design.view( viewObj ) → thisAdds a map and reduce view to the design document under name. Several syntaxes are accepted, depending on if you have a reduce method to set in addition to the map.
design.list()
design.list( name, fn ) → this
design.list( listObj ) → thisAdds a list method to the design document under name. Several list methods can be set using an object.
design.show()
design.show( name, fn ) → this
design.show( showObj ) → thisAdds a show method to the design document under name. Several show methods can be set using an object.
design.update()
design.update( name, fn ) → this
design.update( updateObj ) → thisAdds a update method to the design document under name. Several update methods can be set using an object.
design.filter()
design.filter( name, fn ) → this
design.filter( filterObj ) → thisAdds a filter method to the design document under name. Several filter methods can be set using an object.
design.validate()
design.validate( fn ) → thisAdds a validate_doc_update method to the design document.
design.lib()
design.lib( key, value ) → thisAdds a commonjs module to the design document. This will prefix the value with module.exports = so that it can be required by other methods in the design document. The key uses .set() under the hood so it can be dot-separated for deep values.