Node.js SDK

Fork me on GitHub 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.

Connecting

To connect to a bucket, simply 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 map-reduce 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.