Node.js SDK 2.0/2.1

Node.js SDK 2.0/2.1

The Couchbase Node.js SDK allows you to connect to a Couchbase cluster from Node.js. It is a native Node.js modules and uses Couchbase's high performance C library to handle communicating to the cluster over Couchbase’s binary protocols.

Here's a sampling of what you can do with the Couchbase Node.js SDK:

Connecting

To connect to a bucket, just instantiate a new Cluster object to represent your cluster, and call the openBucket() method, passing in the name of the bucket that you want to connect to. If no bucket name is specified, the default bucket is opened. The following example shows how to connect to a bucket on localhost:

var myCluster = new couchbase.Cluster('couchbase://localhost');
var myBucket = myCluster.openBucket('default');

Operations

In most cases, operations are performed by invoking the relevant method whilst passing the key, any data and meta information that goes with it, along with a callback to invoke when the results are available.

Here is an example of performing a get operations:

myBucket.get('document_name', function(err, res) {
  console.log('Value: ', res.value);
});

Additionally, sometimes more information beyond the key is needed for an operation, as seen here:

myBucket.insert('document_name', {some:'value'}, function(err, res) {
  console.log('Success!');
});

Performing Queries

In addition to the basic operations that are available, we additional provide methods to perform queries against your data. These queries allow you to ask questions like "What beers start with A?" rather than simply "Give me the beer that is called Aaas_Frankl." This is possible through the use of Couchbase's MapReduce views engine. The following is an example of performing a view query.

var query = ViewQuery.from('beer', 'by_name').skip(6).limit(3);;
myBucket.query(query, function(err, results) {
  for(i in results) {
    console.log('Row:', results[i]);
  }
});

Callbacks

The Node.js Couchbase driver uses a callback pattern to notify the application when results are ready. You pass callback functions to the operation methods and the callbacks are invoked when the results or errors are ready. All storage, retrieval and query operations (both singular and batch methods) follow the same callback pattern, but the format of the results differs.