You are viewing the documentation for a prerelease version.

View Latest

Install and Start Using the Node.js SDK with Couchbase Server

The Couchbase Node.js SDK enables you to interact with a Couchbase Server cluster from the Node.js language.

These pages cover the Alpha of the Couchbase Node.js SDK. As such they are likely to change without notice. This alpha code should not be used in production.

Documentation is incomplete, subject to change, and likely to contain broken links.

Installing the SDK

npm install couchbase

Information on new features, fixes, known issues, as well as information on how to install older release versions is in the release notes.

Hello Couchbase

Now you have the Node.js client installed, try out the following:

var cluster = new couchbase.Cluster(
    'http://10.112.180.101:8091', {
        username: 'Administrator',
        password: 'password'});

Couchbase uses Role Based Access Control (RBAC) to control access to resources. Here we will use the Full Admin role created during installation of the Couchbase Data Platform. For production client code, you will want to use more appropriate, restrictive settings, but here we want to get you up and running quickly. If you’re developing client code on the same VM or machine as the Couchbase Server, your URI can be localhost.

// get a bucket reference
var bucket = cluster.bucket('bucket-name');

If you installed the travel sample data bucket, substitute travel-sample for bucket-name.

// get a collection reference
var collection = bucket.defaultCollection();
var myCollection = bucket.collection("my-collection");

The latest release, Mad Hatter, brings a limited Developer Preview of Collections, allowing Documents to be grouped by purpose or theme, according to a specified Collection. Here we will use the DefaultCollection, rather than a specific collection, but best practice would be to group your users into a single collection, groups into another, etc…​

// upsert document
var upsertResult = await collection.upsert("my-document", {name: 'mike'});

// get document
var getResult = await collection.get("my-document");

KV Operations are described in detail on the KV Operations page.

Additional Resources

Couchbase welcomes community contributions to the Node.js SDK. The Node.js SDK source code is available on GitHub.