npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-couchdb

v2.0.0

Published

ES2015-compatible package to interact with CouchDB

Downloads

13,027

Readme

node-couchdb Build Status Dependency Status devDependency Status Greenkeeper badge

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');
// node-couchdb instance with default options
const couch = new NodeCouchDb({
    auth: {
        user: AUTH_USER,
        pass: AUTH_PASS
    }
});

// node-couchdb instance with Memcached
const MemcacheNode = require('node-couchdb-plugin-memcached');
const couchWithMemcache = new NodeCouchDb({
    cache: new MemcacheNode,
    auth: {
        user: AUTH_USER,
        pass: AUTH_PASS
    }
});

// node-couchdb instance talking to external service
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 => {
    // request error occured
});

Drop database

couch.dropDatabase(dbName).then(() => {...}, err => {
    // request error occured
});

List databases

couch.listDatabases().then(dbs => dbs.map(...), err => {
    // request error occured
});

Get document by its id

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
});

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}) => {
    // 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
});

Query using Mango

const dbName = "database";
const mangoQuery = {
    selector: {
        firstname: {
            $gte: 'Ann',
            $lt: 'George'
        }
    }
};

couch.mango(dbName, mangoQuery).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
});

Insert a document

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
});

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
}).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
});

Insert an attachment

couch.insertAttachment("databaseName", "document id", "attachment name", "attachment body", "doc 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=EFIELDMISSING if either _id or _rev fields are missing
});

Delete an attachment

// note that "doc" must have both "_id" and "_rev" fields
couch.update("databaseName", "document id", "attachment name", "doc 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=EFIELDMISSING if either _id or _rev fields are missing
});

Use an update function

couch.updateFunction("databaseName", "designDocument", "updateFunction", {optional query string}, "docid").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
});

Delete a document

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
});

Generate unique identifier(s)

// get one unique id
couch.uniqid().then(ids => ids[0]);

// get N unique ids
couch.uniqid(N).then(ids => ids.map(...));