Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Usage of Cloudant in LoopBack Node.js Applications

DZone's Guide to

Usage of Cloudant in LoopBack Node.js Applications

The Node.js framework LoopBack supports multiple connectors for various data sources. Read on for a sample of how to use the Cloudant NoSQL database in Bluemix.

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

The Node.js framework LoopBack supports multiple connectors for various data sources. Below is a sample of how to use the Cloudant NoSQL database in Bluemix.

Check out the sample code on GitHub. It shouldn’t take longer than 15 minutes to set up the sample application. The sample uses basically a CLEAN stack with Cloudant, LoopBack, Express, Angular, and Node.

After you’ve created the Cloudant service on Bluemix, you get your own credentials. The credentials are stored in environment variables and can be accessed by Bluemix applications. When running the same LoopBack applications locally, the credentials need to be copied to a local property file (env.json). The code in bluemix.js reads the credentials and sets up a LoopBack data source programmatically (it is not defined in datasources.json). The code uses the loopback-connector-cloudant which is installed via npm.

In order to make the setup of the sample application as easy as possible, a database is created automatically if it doesn’t exist and test data is stored in it for demonstration purposes.

In model-config.json, you can refer to the Cloudant database in the same way as to other LoopBack data sources.

"ApprovalRequest": {
  "dataSource": "cloudant",
  "public": true
},
"Person": {
  "dataSource": "cloudant",
  "public": true
}

While loopback-connector-couch uses map/reduce views, loopback-connector-cloudant uses Cloudant Query. My colleague and developer of the Cloudant connector Tony French told me that the main benefit of Cloudant Query is that the filtering/ordering/paging is done on the server vs sending the data across the network.

In the Cloudant dashboard, you can invoke queries (for example, to change test data directly in the database.)

{
  "selector": {
    "username": "Admin"
  },
  "use_index": "_design/lb-index-ddoc-Person"
}

Image title

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:
cloudant ,loopback ,node.js

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}