September 13, 2013

Intro and Couchnode 1.0.0

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 backend, using Couchbase as a backing store (of course!).
 
The reason that I decided today would be a good day to make my first blog post is that I am happy to announce that we have just released the first Couchnode (Which is the name of the Couchbase Node.js driver) general availability release!  Version 1.0.0 of Couchnode was released today, which represents the end of an accelerated release cycle we have had to get Node.js from its previous 0.0.13 ‘new project’ state, up to our general availability release today.
 
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 callback 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 didnt get a chance to add to previous iterations of the Node.js driver is built-in storage durability requirements.  With this feature, you can now request that the SDK internally handle ensuring your durability requirements are met, which is a break from our previous way of handling this where it was a separate operation.
 
bucket.set("foo”, "value", {
    persist_to: 2,
    replicate_to: 1
}, function(err, result) { ... });
 
The above example show a set operation being preformed with a durability requirement specifying that the data must be persisted to disk on at least 2 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 (no code samples :() 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 on these!
 

Enjoy! Brett

Comments