O'Reilly logo

JavaScript JSON Cookbook by Ray Rischpater

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a CouchDB database using Node.js and Cradle

Before you can use a database in CouchDB, you must create it.

How to do it...

Once you've obtained a handle to the database that you want to use, you should check to see whether it exists, and create it if it doesn't:

db.exists(function (err, exists) {
if (err) {
  console.log('error', err);
} elseif (!exists) {
{
  db.create();
}
});

How it works…

The exists method checks to see whether a database exists, calling the callback you provide with an error if one occurred and a flag indicating whether or not the database exists. If the database doesn't exist, you create it using the create method.

This is a common pattern for Cradle because the RESTful interface is, by nature, asynchronous. You'll pass the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required