node-couchdb

node.js package to interact with CouchDB using ETags and your favorite cache layer (memcached, fs, memory, etc)

npm install node-couchdb
9 downloads in the last week
47 downloads in the last month

node-couchdb Travis CI

CouchDB is amazing and easy-to-use NoSQL document-oriented database. This package provides an easy way to interact with CouchDB using ETags and your preferred cache layer (memcached, file system, memory, etc). Check out examples folder for more info.

Installation

npm install node-couchdb

or

npm install --dev

to run examples

API

How to start

// use memory caching
var nodeCouchDB = require("node-couchdb");
var couch = new nodeCouchDB("localhost", 5984);

// even simplier, but you can't set host and port this way
var couch = require("node-couchdb");

// use memcached with "memcache" NPM package
var nodeCouchDB = require("node-couchdb");
var memcacheClient = require("memcache").Client(11211, "localhost");
memcacheClient.on("connect", function () {
    memcacheClient.invalidate = function () {};
    var couch = new nodeCouchDB("localhost", 5984, memcacheClient);
});

memcacheClient.connect();

Fetch document by its id

couch.get("databaseName", "some_document_id", function (err, resData) {
    if (err)
        return console.error(err);

    console.dir(resData);
});

Insert a document

couch.insert("databaseName", {
    "_id" : "document_id",
    "field" : ["sample", "data", true]
}, function (err, resData) {
    if (err)
        return console.error(err);

    console.dir(resData)
});

Update a document

// note that "doc" must have both "_id" and "_rev" fields
couch.update("databaseName, {
    "_id" : "document_id",
    "_rev" : "1-xxx"
    "field" : "new sample data",
    "field2" : 1
}, function (err, resData) {
    if (err)
        return console.error(err);

    console.dir(resData);
});

Delete a document

couch.del("databaseName", "some_document_id", "document_revision", function (err, resData) {
    if (err)
        return console.error(err);

    console.dir(resData);
});

Generate unique identifier(s)

couch.uniqid(1, function (err, ids) { // or even simplier: couch.uniqid(function (err, ids) {
    if (err)
        return console.error(err);

    console.dir(ids);
});

Fetch data by requesting a view

var dbName = "database";
var startKey = ["Ann"];
var endKey = ["George"];
var viewUrl = "_design/list/_views/by_firstname";
var queryOptions = {
    "startkey" : startKey,
    "endkey" : endKey
};

couch.get(dbName, viewUrl, queryOptions, function (err, resData) {
    if (err)
        return console.error(err);

    console.dir(resData)
});

couch.createDatabase(), couch.dropDatabase() and couch.listDatabases() are also available. Check out the sources for more info.

npm loves you