I've been playing around with the Couchbase Node SDK lately and I thought it might be fun to blog my progress towards creating a fully fledged Node app using Couchbase as the database.

Over the course of a few blog posts, I'm going to walk through building a simple Node.js app that uses Couchbase on the back-end. I'll start with the very basics, move onto something that does a little more, introduce views, bring in the Ottoman ODM and then experiment with N1QL.

Installing the SDK

Installation is straightforward:

$ npm install couchbase --save 1 $ npm install couchbase -- save

The client is a native Node wrapper around libcouchbase, the Couchbase C library. If you're on Windows, you'll get everything pre-built. Otherwise you'll need node-gyp and a build environment. You should take a look at the install page to get a full idea of what's necessary for your environment.

Putting something in

Using the Node client you get three ways to put data into Couchbase:

insert: add a new document but give an error if there's already a key with that key (the C in CRUD)

add a new document but give an error if there's already a key with that key (the C in CRUD) replace: overwrite an existing document only and throw an error if there's no existing key of that name (the U in CRUD)

overwrite an existing document only and throw an error if there's no existing key of that name (the U in CRUD) upsert: regardless of whether there's an existing key with that name, write the document (either the C or U of CRUD).

For now, let's create a simple demo using upsert. The tools we'll need are:

Express.js

the body-parser middleware for Express

Couchbase Server running locally

and of course the Couchbase Node client that we just installed.

Connecting

We'll start off by requiring Express and body-parser, assuming you've already installed them with npm.

Next we'll connect first to the Couchbase cluster and then to the default bucket.

For the sake of simplicity, there's no error checking here yet.

Creating a simple form to capture some data

Next up let's create a couple of routes. First we'll have a home page with a simple form and then something to handle the form input.

Then we run the app on port 3000 and, well, not a lot else happens really.

Our form POSTs the input back to the home page but we've done nothing to handle it. So let's fix that.

Handling the form input

As we're POSTing our form input back to the home page, we need to create a POST route to deal with it and we'll pass the urlEncodedParser object to give us the form input in a state we can use. We'll pop it in directly after our first route.

Next, we'll send a 400 error if there's no form data to parse.

Then we'll convert the form data to JSON and save it into a Couchbase document.

There you go. We've put our first document into Couchbase Server from the Node client. The full code is on Github.

Next time we'll sketch out a more useful app and start thinking about how we'll represent the data in Couchbase.