Hello Couchbase Example

To follow the tradition of programming tutorials, the first code example is a “Hello Couchbase” app. This example works with the beer-sample bucket that is provided with Couchbase Server. The following sections contain instructions for running the example app and the annotated Hello Couchbase code.

Running Hello Couchbase

To run the example Hello Couchbase program:

  1. Start Couchbase Server on your computer.
  2. Open the Couchbase administrator console.
  3. Make sure the beer-sample bucket is available by checking for it under the Data Buckets tab. If you need to install the bucket:
    1. Select Settings > Sample Buckets.
    2. Install the beer-sample bucket.
  4. If you haven't already installed the Couchbase Node.js module, install Couchnode.
  5. Copy and paste the Hello Couchbase code into a file named hello-couchbase.js. The code is located in the next section.
  6. In a terminal window, change to the directory that contains the hello-couchbase.js code.
  7. Execute the following command:
    $ node hello-couchbase.js

Hello Couchbase code

The Hello Couchbase example consists of one JavaScript file, hello-couchbase.js. The code opens a connection to Couchbase Server, retrieves a document, modifies the document, and stores the updated document in the database. Here's the hello-couchbase.js code:

hello-couchbase.js

var couchbase = require("couchbase");

// Connect to Couchbase Server

var cluster = new couchbase.Cluster('127.0.0.1:8091');
var bucket = cluster.openBucket('beer-sample', function(err) {
  if (err) {
    // Failed to make a connection to the Couchbase cluster.
    throw err;
  }

  // Retrieve a document

  bucket.get('aass_brewery-juleol', function(err, result) {
    if (err) {
      // Failed to retrieve key
      throw err;
    }

    var doc = result.value;

    console.log(doc.name + ', ABV: ' + doc.abv);
    
    // Store a document

    doc.comment = "Random beer from Norway";

    bucket.replace('aass_brewery-juleol', doc, function(err, result) {
      if (err) {
        // Failed to replace key
        throw err;
      }

      console.log(result);

      // Success!
      process.exit(0);
    });
  });
});

The console output should look similar to this:

Juleøl, ABV: 5.9
{ cas: { '0': 1024851968, '1': 467655457 } }

The following points explain each step in the example:

  • Connecting

    A Bucket object represents a connection to a single bucket within the cluster.

    A bucket represents a logical namespace for a key. All keys must be unique within a single bucket, but multiple buckets can have keys with the same names and they will not conflict. A new connection object must be created for each bucket that you want to interact with in your application. This example creates one connection to the beer-sample bucket.

    The constructor is passed the bucket name, which is beer-sample, and a node on the cluster to connect to. You can pass any node that is a member of the cluster. This example uses the local cluster instance.

  • Retrieving Data

    The get() method initiates an asynchronous request to retrieve the specified key. If the key exists, the callback is invoked with a results object that contains the value of the key and any additional metadata returned from the cluster. To get the actual value of the object, you can access the result object’s value property.

    If the key does not exist on the server, the callback is invoked with an unset value property on the result object (it will not set the callbacks error parameter).

  • Storing Data

    To store documents in the server, you can use one of the set family of methods. This example uses the replace() method, which enforces the constraint that a previous value of the document must already exist. This can be thought of as an update operation in terms of CRUD (create, read, update, delete) operations.

    The storage methods also return a result object that contains metadata about the value that was stored.