node-couchdb
node-couchdb
package provides an easy way to interact with CouchDB using preferred cache layer:
Installation
npm install node-couchdb --save
API
Constructor
node-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.
ES Module:
import NodeCouchDb from 'node-couchdb';
Common JS:
const NodeCouchDb = require('node-couchdb');
const couch = new NodeCouchDb({
auth: {
user: AUTH_USER,
pass: AUTH_PASS
}
});
const MemcacheNode = require('node-couchdb-plugin-memcached');
const couchWithMemcache = new NodeCouchDb({
cache: new MemcacheNode,
auth: {
user: AUTH_USER,
pass: AUTH_PASS
}
});
const couchExternal = new NodeCouchDb({
host: 'couchdb.external.service',
protocol: 'https',
port: 6984,
auth: {
user: AUTH_USER,
pass: AUTH_PASS
}
});
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.
Create database
couch.createDatabase(dbName).then(() => {...}, err => {
});
Drop database
couch.dropDatabase(dbName).then(() => {...}, err => {
});
List databases
couch.listDatabases().then(dbs => dbs.map(...), err => {
});
Get document by its id
couch.get("databaseName", "some_document_id").then(({data, headers, status}) => {
}, err => {
});
Get view results
const dbName = "database";
const startKey = ["Ann"];
const endKey = ["George"];
const viewUrl = "_design/list/_view/by_firstname";
const queryOptions = {
startkey: startKey,
endkey: endKey
};
couch.get(dbName, viewUrl, queryOptions).then(({data, headers, status}) => {
}, err => {
});
Query using Mango
const dbName = "database";
const mangoQuery = {
selector: {
firstname: {
$gte: 'Ann',
$lt: 'George'
}
}
};
couch.mango(dbName, mangoQuery).then(({data, headers, status}) => {
}, err => {
});
Insert a document
couch.insert("databaseName", {
_id: "document_id",
field: ["sample", "data", true]
}).then(({data, headers, status}) => {
}, err => {
});
Update a document
couch.update("databaseName", {
_id: "document_id",
_rev: "1-xxx"
field: "new sample data",
field2: 1
}).then(({data, headers, status}) => {
}, err => {
});
Insert an attachment
couch.insertAttachment("databaseName", "document id", "attachment name", "attachment body", "doc revision").then(({data, headers, status}) => {
}, err => {
});
Delete an attachment
couch.update("databaseName", "document id", "attachment name", "doc revision").then(({data, headers, status}) => {
}, err => {
});
Use an update function
couch.updateFunction("databaseName", "designDocument", "updateFunction", {optional query string}, "docid").then(({data, headers, status}) => {
}, err => {
});
Delete a document
couch.del("databaseName", "some_document_id", "document_revision").then(({data, headers, status}) => {
}, err => {
});
Generate unique identifier(s)
couch.uniqid().then(ids => ids[0]);
couch.uniqid(N).then(ids => ids.map(...));