Getting started with CouchDB and NodeJS on Ubuntu 10.10

Post to Twitter

My previous post showed you how to install CouchDB on Ubuntu 10.10 and use PHP 5 to test it. Today I’ll be doing the same thing but instead of PHP I’ll be using NodeJS. If you need to install CouchDB then refer to my previous post, if you need to install NodeJS (and NPM) then refer to this post. You will need NPM installed since we will be using the Cradle client to get NodeJS talking to CouchDB.


Assuming now that CouchDB and NodeJS are installed, you then need to install Cradle via NPM:

$ npm install cradle

As you work through the code you can verify things by using Futon which is a browser based front end to CouchDB. You can access it here (assuming you haven’t changed the defaults): http://127.0.0.1:5984/_utils/

To create a new database and to insert some documents I’ve created the following script based off the Cradle documentation:

Filename: cradle-demo-1.js

var cradle = require('cradle'),
        sys = require('sys');

// Defaults to 127.0.0.1:5984
var conn = new(cradle.Connection)();
// Create a new database
var db = conn.database('gadgets');
db.create();

sys.puts('Database Created');

// Insert a single document
db.save({name: 'Kindle', company: 'Amazon'}, function (err, res) {
    sys.puts(res);
    sys.puts('Kindle entered into gadget database');
});

// Insert multiple documents
db.save([{name: 'Xoom', company: 'Motorola'}, {name: 'iPad', company: 'Apple'}], function (err, res) {
    sys.puts(res);
    sys.puts('Xoom and iPad entered into gadget database');
});

Running the program is done like this:

$ node cradle-demo-1.js

Results:

First, the database is created. Next, I inserted one document, then showed you how to insert two documents. The code is pretty simple.

The next script will get the Kindle document from the database.

Filename: cradle-demo-2.js

var cradle = require('cradle'),
        sys = require('sys');

// Defaults to 127.0.0.1:5984
var conn = new(cradle.Connection)();
var db = conn.database('gadgets');

// NOTE: Update this to an id in your database
var kindleDocId = 'c171b1fd94efb863673c1d33a400b1ce';

db.get(kindleDocId, function (err, doc) {
    sys.puts(doc);
    sys.puts('Fetched this gadget: ' + doc.name);
});

Results:

The final example is updating a document.

Filename: cradle-demo-3.js

var cradle = require('cradle'),
        sys = require('sys');

// Defaults to 127.0.0.1:5984
var conn = new(cradle.Connection)();
var db = conn.database('gadgets');

// NOTE: Update this to an id in your database
var kindleDocId = 'c171b1fd94efb863673c1d33a400b1ce';

db.save(kindleDocId, {name: 'Kindle3', company: 'Amazon', ceo: 'Jeff Bezos'}, function (err, res) {
  db.get(kindleDocId, function (err, doc) {
      sys.puts('Updated: ' + doc);
  });
});

Results:

To delete an item simply do the following:

db.remove('PUT-YOUR-ID-HERE', function (err, res) {
    // Handle response
});

Or with a revision id handy do this:

db.remove('PUT-YOUR-ID-HERE', 'PUT-YOUR-REV-ID-HERE', function (err, res) {
    // Handle response
});

Here is an example of inserting an item and then removing it right away:

var cradle = require('cradle'),
        sys = require('sys');

// Defaults to 127.0.0.1:5984
var conn = new(cradle.Connection)();
// Create a new database
var db = conn.database('gadgets');
db.create();

sys.puts('Database Created');

db.save({name: 'Kindle', company: 'Amazon'}, function (err, res) {
    sys.puts(res);
    sys.puts('Kindle entered into gadget database');
    
    // Now remove it
    removeId(res.id);
    // Remove with a revision id
    // removeId(res.id, res.rev);
});

function removeId(id, revId)
{
    db.remove(id, revId, function (err, res) {
        sys.puts(res);
        sys.puts('Kindle removed from gadget database');
    });
}

function removeId(id)
{
    db.remove(id, function (err, res) {
        sys.puts(res);
        sys.puts('Kindle removed from gadget database');
    });
}

I was able to remove the item with or without the revision id in that example.

// Remove it with no revision id given
removeId(res.id);

// Remove it with a revision id
removeId(res.id, res.rev);

Keep in mind this is just the tip of the iceberg of what CouchDB and the Cradle client are capable of. Read the documentation for each to discover the rest.

Post to Twitter

This entry was posted in CouchDB, Node.js, Open Source, Ubuntu. Bookmark the permalink.

2 Responses to Getting started with CouchDB and NodeJS on Ubuntu 10.10

  1. Hello,

    The tutorials have been great so far but I think you have an error in the delete example. Using the above code, node was tossing errors. After reading through the Cradle docs, I believe the code should be:

    db.remove(‘gadgets’, ‘PUT-YOUR-ID-HERE’, function (err, res) {
    // Handle response
    });

    Thanks again for some easy, bite-sized tutorials on CouchDB and NodeJS,

    -Mike

  2. Chad Lung says:

    @Mike,

    I just tried the code without a revision id and I was able to remove the item. According to the docs “If remove is called without a revision, and the document was recently fetched from the database, it will attempt to use the cached document’s revision, providing caching is enabled.”

    I’ve updated my article with an example to remove an item to clarify, thanks.

    Chad

Comments are closed.