NoSQL Zone is brought to you in partnership with:

Don Pinto is a Product Marketing Manager with experience in cloud and database technologies. Don is a DZone MVB and is not an employee of DZone and has posted 88 posts at DZone. You can read more from them at their website. View Full User Profile

Intro and Couchnode 1.0.0

09.23.2013
| 1216 views |
  • submit to reddit

Originally authored by Brett Lawson

Hey Everyone!

My name is Brett Lawson and I am the new Couchbase Node.js SDK developer. I have worked for Couchbase for about 3 weeks now, but I haven’t had a chance to get a blog out until now. I have been a regular contributor to the Node.js SDK for over 10 months now.

I bring experience with me from the social games industry, where I wrote the service powering our social game service back end, using Couchbase as a backing store (of course!).

The reason that I decided today would be a good day to write my first blog post is because Couchbase just released Couchnode, which is the name of the Couchbase Node.js driver. Version 1.0.0 of Couchnode represents the end of an accelerated release cycle we have had in order to get Node.js from its previous 0.0.13 ‘new project’ state, up to our general availability release.

Now, lets get on with some code samples on how to get started with the new Couchnode SDK!

Connecting to a Couchbase cluster is a fairly simple procedure. You simply instantiate a new connection object passing in your options as an object and you are set to go. You can optionally pass a callback to the constructor, but this is not necessary and any operations performed against a connection that is still opening will be queued and executed as soon as the connection is available.
 var bucket = new couchbase.Connection({
  bucket:"default",
  password:"",
  host:"localhost:8091"
});
Once you are connected, you can start performing some operations. Here is an example of us setting a new key into our cluster and then immediately retrieving it afterwards:
 bucket.set("foo", "val", function(err, result) {
  if (err) throw err;
  bucket.get("foo", function(err, result) {
    if (err) throw err;
    console.log(result.value);
  });
});
Those of you who have used previous iterations of Couchnode may notice that our API has changed. This has been to make all of our callbacks a bit more uniform and easier to use and wrap.

Another interesting feature is one we have had in our other SDKs for a while now, but didn't get a chance to add to previous iterations of the Node.js driver.  The feature is built-in storage durability requirements. With this feature, you can now request that the SDK internally handle the requirements, ensuring your durability requirements are met.  This is a break from our old way of handling requirements, which were previously run as separate operations.
 bucket.set("foo”, "value", {
    persist_to: 2,
    replicate_to: 1
}, function(err, result) { ... });
The above example shows a set operation being preformed with a durability requirement specifying that the data must be persisted to disk on at least two nodes, and additionally, must be replicated to, at minimum, one additional node other than the master. This can be a powerful and simple way to ensure the integrity of your data and can make working with views much simpler.

The last thing I would like to mention is the fact that we have refactored our view API significantly as well. We have done this to allow support for a new paging API that we have also included in the newer versions of the SDK, which can make building paginated web services extremely simple. I suggest you take a look at our API reference or SDK manual for more information!
Published at DZone with permission of Don Pinto, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)