Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including couche with all npm packages installed. Try it out:

var couche = require("couche")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

couche v0.0.4

couchdb client for node, with specific apis

Node Couchdb Client - Couche

Build Status

There are already many couchdb client in npm, and some of them are great project, but still not implements all the couchdb features that satisfied my needs in auth and flexibility, and not updated for one year. Some other libs has fewer apis and failed to meet needs. Even some are not complete yet or not freindly to use.

Installation

npm install couche --save

Usage

Create a couch server:

var couche = require('couche'),
    server = couche('http://localhost:5984');

Or

var CouchDB = require('couche').CouchDB;
    server = new CouchDB('http://localhost:5984');

You can pass username && password to auth() make authentication:

server.auth(username, password);

Or you can utilize the session by login:

server.login(username, password, function(err) {
    // do admin ops
    ....
    server.logout(function(err) {
        // final work
    });
});


Get a database:

``` js
var db = server.database('couche');

Or using bind:

server.bind('couche');
var db = server.couche;

// destroy
server.unbind('couche');

You can extend database:

db.bind({
   // read documents by page
   page: function(n, limit, callback) {
       // Don't use skip/limit do page on views, see http://docs.couchdb.org/en/1.5.x/couchapp/views/pagination.html#views-pagination
       return this.select().skip((n-1)*limit).limit(limit|| this.defaultLimit).exec(callback);
   },
   defaultLimit: 20
});


db.page(1, 20, function(err, rows) {
    // get page items
});

Create database and insert new doc

var server = require('couche')('http://localhost:5984');

var db = server.database('test');
db.destroy(function(err) {
    // create a new database
    db.create(function(err) {
        // insert a document with id 'jack johns'
        db.insert({ _id: 'jack johns', name: 'jack' }, function(err, body) {
            if (err) {
                console.log('insertion failed ', err.message);
                return;
            }
            console.log(body);
            // body will like following:
            //   { ok: true,
            //     id: 'jack johns',
            //     rev: '1-610953b93b8bf1bae12427e2de181307' }
        });
    });
});

APIs

Server

Database

Document

Config

License

(The BSD License)

Copyright (c) 2014, Villa.Gao <jky239@gmail.com>;
All rights reserved.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free