Skip to content

Database

Dominic Barnes edited this page Aug 11, 2014 · 3 revisions

Database(server, [name])

Represents a CouchDB database. Like the Server object, the methods here are largely administrative.

var couchdb = require("couchdb-api");

var db = couchdb().db("mydb");

Database#url([path])

This helper method is for generating a String URL relative to the database. If path is specified, it will resolve that path relative to the server root. (therefore, beginning a path with / will make the resolved path relative to the server itself) path can also be an Array:String.

var base = db.url();
// => http://localhost:5984/mydb

var doc = db.url("mydoc");
// => http://localhost:5984/mydb/mydoc

var root = db.url("/");
// => http://localhost:5984/

Database#doc(id, rev)

This helper method creates and returns a new Document object based on this Database.

Both parameters are technically optional, see the Document API documentation for more information.

var doc = db.doc("mydoc");

Database#ddoc(name, rev)

This helper method creates and returns a new DesignDocument object based on this Database.

Both parameters are technically optional, see the Document API documentation for more information.

var ddoc = db.ddoc("myddoc");

Database#ldoc(name, rev)

This helper method creates and returns a new LocalDocument object based on this Database.

Both parameters are technically optional, see the Document API documentation for more information.

var ldoc = db.ldoc("myldoc");

Database#create(callback)

PUT /{db} CouchDB Documentation

Creates the database and returns the results.

db.create(function (err, results) {
    // ...
});

Database#destroy(callback)

DELETE /{db} CouchDB Documentation

Deletes the database.

db.destroy(function (err, results) {
    // ...
});

Database#exists(callback)

HEAD /{db} CouchDB Documentation

Checks for the database's existence.

db.exists(function (err, exists) {
    // ...
});

Database#info(callback)

GET /{db} CouchDB Documentation

Retrieves information about this database.

db.info(function (err, info) {
    // ...
});

Database#allDocs(query, callback)

Queries a view consisting of all documents in the database. When only a callback is provided: it is a simple GET. When an Array is provided, it will issue a POST with { keys: [ ... ] }. When an Object is provided, a GET request will be issued with additional query-string arguments appended.

// simple query
db.allDocs(function (err, results) {
    // ...
});

// with query-string params
db.allDocs({ stale: true, skip: 10 }, function (err, results) {
    // ...
});

// with an array of document keys
db.allDocs([ "doc-1", "doc-2" ], function (err, results) {
    // ...
});