Starter NodeJS & Cloudant

Cloudant DB

We are using CloudantDB running on Bluemix. Cloudant is built on CouchDB and comes with its own REST APIs to access/add/manipulate data. However, there are third party Cloudant libraries that simplify interactions with the database.

We are using objective-cloudant in our iOS project and java-cloudant in our android project.

Setting Up Your Project

Download Source Code

  1. Download the server/nodejs source code: https://github.com/health-tech-hack/starter-nodejs-server

Bluemix Account Setup

  1. Sign up for a Bluemix account: https://console.ng.bluemix.net/registration/
  2. From the Bluemix Console, click on Catalog (in the top Navigation Bar)
  3. Click on Node.js Cloudant DB Web Starter, Enter your app name, and click on Create
  4. This will setup your Node JS Server and a instance of Cloudant DB.

Setup Cloudant DB

  1. Go to your dashboard and click on your Cloudant DB instance, under the Services section.
  2. Make a note of your Cloudant DB Instance name, e.g. starter-app-cloudantNoSQLDB
  3. Select Service Credentials from the left navigation pane, and Click on Add Credentials.
  4. Name your credentials and copy your credentials json text to a temporary location.
  5. Click on Launch on the top right corner to launch your Cloudant DB Admin panel.
  6. Create a database called sensor_data
  7. Select the database and create a Query Index. This is located in the dropdown menu under Design Document (click the plus icon). Copy and paste the text below and click on Create Index.
{
  "index": {
    "fields": [],
     "selector": {},
    "default_analyzer": "keyword",
    "default_field": {},
    "index_array_lengths": true
  },
  "type": "text"
}

> more info about indexes


NodeJS Server Setup

There are a few ways to get you codebase setup. We will get you setup using the command line interface.

  1. In your app dashboard, click on Start Coding (on the left navigation pane) and Select CF - Command Line Interface.
  2. You will need to configure the source code that you downloaded above:
    1. Edit the manifest.yml file and update your app/host name to your app/host name instance. Under services, update your cloudant db service name to your Cloudant Db Instance name that you found above.
    2. Create a folder called config and a file inside that folder called local-db-info.json. Edit the file and add the text below. Make sure to fill in your db credentials json obj where instructed.

template:

  "credentials": {YOUR_CREDENTIALS_JSON_OBJ_HERE},
  "dbname": "sensor_data"
}

sample filled out template:

    "credentials": {
        "username": "test-bluemix",
        "password": "test",
        "host": "test-bluemix.cloudant.com",
        "port": 443,
        "url": "https://test-bluemix:[email protected]"
    },
    "dbname": "sensor_data"
}

3) Follow the steps, starting from step 2, in the CF - Command Line Interface guide. Instead of using the source code from step 1, you will use the source code that you downloaded earlier.

Data Format

Use this data format (below) when uploading data. The actual data values go inside the inner dictionary with the key data. This is to support multiple data values. For example, blood pressure contains a systolic and a diastolic value.

insertionDateInSeconds is the date the record was inserted. dateInSeconds is the date that the value was recorded. For example, the user could have recorded a weight value on Thursday (dateInSeconds) but it was uploaded to the server on Friday (insertionDateInSeconds).

Data Format:

{
  "_id": "4FF1B170-4DE8-488C-AF11-91050B29FD09", //{auto_generated}
  "_rev": "1-f62f97e5a6aa60b9cb831e6c78f3588a", //{auto_generated}
  "dateInSeconds": 1466968380,
  "healthObjType": "Weight",
  "sourceName": "Health",
  "insertionDateInSeconds": 1466970860.109398,
  "data": {
    "value": 133
  }
}

results matching ""

    No results matching ""