couch-promised
A promise-based wrapper around CouchDB. CouchPromised does not aim to be a full- featured CouchDB client and instead aims to provide a minimal set of methods for some of the most common interactions.
Installation
The package is available through npm and can be installed as usual:
npm install --save couch-promised
Once installed you can import the package into your script as usual:
var CouchPromised = ;
Usage
The CouchPromised
variable above refers to a constructor function. You must
create instance before you can interact with a database:
var couch = host: 'example.com' // Your CouchDB server. Defaults to 127.0.0.1. port: 5984 // Port of CouchDB server. Defaults to 5984. path: '/my-database' // Path to a database stored on the CouchDB instance.;
API
As mentioned previously, CouchPromised does not attempt to cover all possible
CouchDB functionality. At its heart is the request
method, to which all other
methods delegate. If you need more fine-grained control over a request you may
find that method useful.
get( id )
Find an individual document by unique identifier:
couch;
fetch( ...ids )
Find many documents by unique identifiers. Takes an array or alternatively any number of separate parameters:
couch;
insert( doc )
Create a new document. If the provided object has an _id
property that does
not already exist in the database the new document will be created with that
identifier. If a document already exists and the provided object doesn't have
a valid _ref
property an error will be thrown. The update
method is better
suited to that situation.
couch;
update( doc )
Update an existing document. The provided object must have an _id
property
that corresponds to a stored document and a _rev
property that matches the
latest revision of that stored document.
couch;
destroy( doc )
Delete an existing document. The provided object must have an _id
property
that corresponds to a stored document and a _rev
property that matches the
latest revision of that stored document.
couch;
view( designDocument , viewName [, viewParameters ] )
Query a database view. Requires the name of a design document and the name of a view within that document. Optionally also takes a map of view parameters to their values. The parameters object can contain any of the parameters a CouchDB view normally accepts. It is converted to a querystring and appended to the request URL.
couch;
viewDocs( designDocument , viewName [, viewParameters ] )
Query a database view and return the document associated with each result. This
is effectively sugar around a call to view
with the include_docs
parameter
set.
couch;
bulk( docs )
Perform a bulk insert or update of documents. Requires an array of documents.
Any element of that array with an _id
property that corresponds to a document
already stored in the database must also have the correct _rev
property.
couch;
request( method , path [, body ] )
Send an HTTP request to the CouchDB database. Requires an HTTP method and a URL path. Optionally also takes an object representing the request body.
couch;