node-couchdb package provides an easy way to interact with CouchDB using preferred cache layer:
- process memory
- memcached
- place for your plugin :)
npm install node-couchdb --savenode-couchdb exports constructor, which accepts one object argument with properties host (127.0.0.1 by default), port (5984 by default), protocol (http by default), cache (one of plugins, null by default), auth (object with properties {user, pass}) and timeout for all requests (5000 by default). All object fields are optional.
constNodeCouchDb=require('node-couchdb');// node-couchdb instance with default optionsconstcouch=newNodeCouchDb();// node-couchdb instance with MemcachedconstMemcacheNode=require('node-couchdb-plugin-memcached');constcouchWithMemcache=newNodeCouchDb({cache: newMemcacheNode});// node-couchdb instance talking to external serviceconstcouchExternal=newNodeCouchDb({host: 'couchdb.external.service',protocol: 'https',port: 6984});// not admin partyconstcouchAuth=newNodeCouchDb({auth: {user: 'login',pass: 'secret'}});All node-couchdb methods return Promise instances which resolve if everything works as expected and reject with Error instance which usually has code and body fields. See package source and tests for more info.
couch.createDatabase(dbName).then(()=>{...},err=>{// request error occured});couch.dropDatabase(dbName).then(()=>{...},err=>{// request error occured});couch.listDatabases().then(dbs=>dbs.map(...),err=>{// request error occured});couch.get("databaseName","some_document_id").then(({data, headers, status})=>{// data is json response// headers is an object with all response headers// status is statusCode number},err=>{// either request error occured// ...or err.code=EDOCMISSING if document is missing// ...or err.code=EUNKNOWN if statusCode is unexpected});constdbName="database";conststartKey=["Ann"];constendKey=["George"];constviewUrl="_design/list/_view/by_firstname";constqueryOptions={
startKey,
endKey
};couch.get(dbName,viewUrl,queryOptions).then(({data, headers, status})=>{// data is json response// headers is an object with all response headers// status is statusCode number},err=>{// either request error occured// ...or err.code=EDOCMISSING if document is missing// ...or err.code=EUNKNOWN if statusCode is unexpected});couch.insert("databaseName",{_id: "document_id",field: ["sample","data",true]}).then(({data, headers, status})=>{// data is json response// headers is an object with all response headers// status is statusCode number},err=>{// either request error occured// ...or err.code=EDOCCONFLICT if document with the same id already exists});// note that "doc" must have both "_id" and "_rev" fieldscouch.update("databaseName",{_id: "document_id",_rev: "1-xxx"field: "new sample data",field2: 1}).then(({data, headers, status})=>{// data is json response// headers is an object with all response headers// status is statusCode number},err=>{// either request error occured// ...or err.code=EFIELDMISSING if either _id or _rev fields are missing});couch.del("databaseName","some_document_id","document_revision").then(({data, headers, status})=>{// data is json response// headers is an object with all response headers// status is statusCode number},err=>{// either request error occured// ...or err.code=EDOCMISSING if document does not exist// ...or err.code=EUNKNOWN if response status code is unexpected});// get one unique idcouch.uniqid().then(ids=>ids[0]);// get N unique idscouch.uniqid(N).then(ids=>ids.map(...));