recumbent
recumbent is a Couch DB client for Node.js.
Usage
To use recumbent, install the library for your application.
$ npm install recumbent
Connecting to a server
var recumbent = ;var server = url: 'http://localhost: 5984' database: 'my_stuff' ;server;
In all classes, you may either pass a Server
object or url
and database
attributes.
Creating a new database
var recumbent = ;var database = url: 'http://localhost:5984' database: 'my_stuff' ;database;
Destroying a database
var recumbent = ;var database = url: 'http://localhost:5984' database: 'my_stuff' ;database;
Inserting a document
To insert a document, create an instance of a Writer
.
var recumbent = ;var writer = server: server ;var obj =message: "Hello, World!";writerdataobj;
Updating a document
Updating a document is identical to inserting a document. The only difference
is that updated documents must have a _rev
attribute. Use the data()
function
to set the data to be sent to the server.
var writer = server: server ;var obj =_id: '04bb33dc698297b4806062feae00cb93'_rev: '1-4439af2c7634656dc6f6397eb4c2259b'message: "Déjà vu";writerdataobj;
Attempting to write a document with a duplicate ID but without giving a revision will result in an error.
Querying for a document by ID
To query for documents, create a new instance of the Query
object. Use the doc()
function to set the document ID to fetch.
var query = server: server ;query;
Creating a design document
Design documents are not different from any other type of document in Couch. Therefore, creating design documents is the same as creating any other document.
var designDocument =_id: '_design/employees'description: 'All views and filters related to employee data.'views:{if doctype === 'employee';}language: 'javascript';var writer = options;writerdatadesignDocument;
Once the document has been created, you can query with the ddoc()
and view()
functions. Queries may be augmented with key
, startkey
, endkey
, skip
, and
limit
options.
var query = options;// query a viewquery;// query a view by keyquery;// query a view by startkey and endkeyquery;// query a view by skip and limitquery;
Adding an attachment to a document
Attachments can be added to existing documents in two ways.
First, add the _attachments
attribute to an existing document and write the document
as described above.
var recumbent = ;var docWithAttachment =description: "This document has an attachment."_attachments:"test.txt":content_type: "text/plain"data: "VGhpcyBpcyBteSB0ZXh0IGRvY3VtZW50";var writer = options;writerdatadocWithAttachment;
As with other updates, if the document is existing, then it must have a _rev
attribute.
Second, use the Attachment
object to put a new attachment on an existing document.
var recumbent = ;var att = options;attnamesomeAttachmentNamerevrevision;attcontentattachmentContent;att;
Getting an attachment
var recumbent = ;var att = options;attnamesomeAttachmentName;
Development
Running Tests
$ npm test