Cloudant and Node.js Made Simple with the cloudant-quickstart (formerly silverlining) Library

A library for new users

--

Editor’s note: In February 2018, the silverlining library has been deprecated and replaced with cloudant-quickstart: https://www.npmjs.com/package/cloudant-quickstart

I frequently talk with developers who are using IBM Cloudant or Apache CouchDB™ for the first time, and I’ve found that they most often have a difficult time coming to terms with these concepts:

  • Design Documents: a feature that is unique to this database family. They are special JSON documents holding index definitions and other data-manipulation magic. Once mastered, design docs can be used to great effect, but to a first-timer they are baffling.
  • Multi-Version Concurrency Control: MVCC is the mechanism whereby documents are stored in a tree of revisions, allowing databases to sync changes with other copies without data loss.

Usually, developers want to learn the mechanics of database CRUD and basic queries. They don’t want to have to learn about design documents in order to query or aggregate data, nor do they want MVCC getting in the way of storing and updating their data. What would Cloudant or CouchDB look like without these concepts? Or without show & list functions, update handlers, attachments, replication, and other advanced features?

To find out, I wrote the silverlining library that hides away this powerful-but-esoteric functionality and concentrates on making data manipulation simple and intuitive.

“Hi ho silver lining, and away you go …”

Introducing silverlining

The silverlining library is a Node.js module that can be used with Cloudant and allows:

  • database creation
  • document insert, update, bulk insert and delete
  • document get, multi-get and bulk fetch
  • database querying
  • aggregation for counting, totalising and statistics

All of this is achieved without users dealing with any of Cloudant’s inner complexities.

Installing

Use the library in your own project like so:

Then, use it within your code by adding the URL of your Cloudant instance and defining which Cloudant database you want to work with:

Creating a database

A new Cloudant database can be created on first-use with the create function:

All the function calls we make here are going to be asynchronous: they return a Promise, which you handle with then and catch functions, but for brevity, I’ve omitted much of this scaffolding from the code samples. For example, to log the output of the info function you could do:

Now, we are going to import some data from geonames.org, which stores the location and population of cities around the world.

Inserting data

Single documents can be added to a database with the insert function:

If an _id property is supplied, then this field becomes the key field of the JSON document stored in Cloudant. If omitted, an '_id' is automatically generated by the database.

The same function can be used to insert multiple documents — simply pass in an array of objects instead:

The array of data can be as long as you like. Let’s get some data from Github and store it in a local file:

It can then be imported in batches with one function call:

Records can be retrieved by their IDs with the get function singly:

Or get records in batches by providing an array of IDs:

Individual records can be updated by supplying the ID and a new document body to the update function (or just a new object):

A document can be deleted with the del function:

Assuming we have managed to create a suitable data set, then we next need to query it.

Querying data

The database can be queried by any field using the query function, passing in a query object:

The query can also contain Cloudant Query selector operators:

Aggregation

Create aggregated views of your data with the count, sum and stats functions.

The count function simply counts things:

It can also count things grouped by other fields within the document:

The sum function calculates totals:

You can also use sum to group the totals by other fields:

The stats function calculates statistics on one or more fields, which can be grouped by other fields:

How does this work?

It’s still Cloudant under-the-hood, but this library is taking care of a few details on your behalf:

  • When you create a database, a Cloudant Query index is created that indexes all fields. This index then powers the query operator.
  • The update and delete functions hide revision tokens from you — behind the scenes these operations fetch the latest revision, overwriting the data with your supplied document. If it doesn't succeed initially, it tries again later, up to three times, at increasingly longer time intervals.
  • The count, sum and stats functions use Cloudant's built-in MapReduce engine, creating the required Design Document if necessary.

In production systems, it’s important to understand design documents, MVCC and the other features of Cloudant, but when you’re getting started it’s helpful to be able to write data quickly and create queries without any fuss.

Is this free?

Yes! This is a free, open-source library that can work with any Cloudant account.

Feel free to give it a go, and we’d welcome any feedback through the project’s GitHub Issues page.

--

--