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
- Download the server/nodejs source code: https://github.com/health-tech-hack/starter-nodejs-server
Bluemix Account Setup
- Sign up for a Bluemix account: https://console.ng.bluemix.net/registration/
- From the Bluemix Console, click on
Catalog
(in the top Navigation Bar) - Click on
Node.js Cloudant DB Web Starter
, Enter your app name, and click onCreate
- This will setup your Node JS Server and a instance of Cloudant DB.
Setup Cloudant DB
- Go to your dashboard and click on your Cloudant DB instance, under the
Services
section. - Make a note of your Cloudant DB Instance name, e.g.
starter-app-cloudantNoSQLDB
- Select
Service Credentials
from the left navigation pane, and Click onAdd Credentials
. - Name your credentials and copy your credentials json text to a temporary location.
- Click on
Launch
on the top right corner to launch your Cloudant DB Admin panel. - Create a database called
sensor_data
- Select the database and create a
Query Index
. This is located in the dropdown menu underDesign Document
(click the plus icon). Copy and paste the text below and click onCreate 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.
- In your app dashboard, click on
Start Coding
(on the left navigation pane) and SelectCF - Command Line Interface
. - You will need to configure the source code that you downloaded above:
- 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.
- Create a folder called
config
and a file inside that folder calledlocal-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
}
}