Skip to content

Instantly share code, notes, and snippets.

@acutetech
Created March 14, 2017 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acutetech/411bab7d9872bac32ad1ebba06c14a37 to your computer and use it in GitHub Desktop.
Save acutetech/411bab7d9872bac32ad1ebba06c14a37 to your computer and use it in GitHub Desktop.
Simple example of working with Cloudant NoSQL database

This is a simple flow that gets you started with storing and retrieving documents from a Cloudant database. Cloudant is based on CouchDB NoSQL database.

It is borrowed from this tutorial by Dave Krier which you should watch first: https://www.youtube.com/watch?v=FXm2Bk-tCIc

It uses "node-red-node-cf-cloudant" Cloudant nodes which are documented here - you can install node-red-node-cf-cloudant if you need to: http://flows.nodered.org/node/node-red-node-cf-cloudant

You can sign up for a free Cloudant database here: https://cloudant.com/

After signing up you should create a database then click on the "Permissions" tab to create a user. Click the "Generate API Key" button and record the key and password values.

Configure the Cloudant nodes by entering the path to your Cloudant account in the Host field. The key and password should be entered in the Username and Password fields. Enter the database name in the Database field.

Deploy your Node-red flow and open another browser tab to execute create, read and bulk delete operations:

(1) To insert documents use commands of this type: http://localhost:1880/putdata?name=graham&age=29

(2) To retrieve documents: http://localhost:1880/getdata

(3) To delete ALL documents: http://localhost:1880/deleteall

TODO - find a more efficient way to delete all the documents.

[{"id":"b803669c.dbaa88","type":"cloudant in","z":"b4bbf6f9.5517a","name":"Retrive docouments","cloudant":"f42e6b50.00d088","database":"mmm_test_1","service":"_ext_","search":"_all_","design":"","index":"","x":398.3333435058594,"y":204.33334350585938,"wires":[["3e155446.233704"]]},{"id":"ae20a281.33b0e","type":"http in","z":"b4bbf6f9.5517a","name":"Insert data","url":"/putdata","method":"get","swaggerDoc":"","x":106.33332824707031,"y":100.18333435058594,"wires":[["bac58887.f9deb","b6f9b0fe.276f2"]]},{"id":"a5b4fcc2.a7a3b","type":"http in","z":"b4bbf6f9.5517a","name":"Retrieve data","url":"/getdata","method":"get","swaggerDoc":"","x":112.33332824707031,"y":204.18331909179688,"wires":[["b803669c.dbaa88"]]},{"id":"bac58887.f9deb","type":"http response","z":"b4bbf6f9.5517a","name":"Data inserted","x":664.3333740234375,"y":101.76666259765625,"wires":[]},{"id":"3e155446.233704","type":"http response","z":"b4bbf6f9.5517a","name":"List of documents","x":672.3333740234375,"y":202.76666259765625,"wires":[]},{"id":"b6f9b0fe.276f2","type":"cloudant out","z":"b4bbf6f9.5517a","name":"Insert test documents","cloudant":"f42e6b50.00d088","database":"mmm_test_1","service":"_ext_","payonly":true,"operation":"insert","x":399.3334045410156,"y":148.13333129882812,"wires":[]},{"id":"1128708.21fc29","type":"comment","z":"b4bbf6f9.5517a","name":"Using CloudantDB in Node-RED","info":"This is a simple flow that tests storing and retrieving documents from a \nCloudant database. It is borrowed from this tutorial by Dave Krier:\nhttps://www.youtube.com/watch?v=FXm2Bk-tCIc\n\nIt uses \"node-red-node-cf-cloudant\" Cloudant nodes which are documented\nhere:\nhttp://flows.nodered.org/node/node-red-node-cf-cloudant\n\nYou can sign up for a free Cloudant database here:\nhttps://cloudant.com/\n\nAfter signing up you should create a database then click on the\n\"Permissions\" tab to create a user. Click the \"Generate API Key\" button\nand record the key and password values.\n\nConfigure the Cloudant nodes by entering the path to the Cloudant database \nin the Host field. The key and password should be entered in the \nUsername and Password fields. Enter the database name in the Database field.\n\nDeploy your Node-red flow and open another browser tab to execute\ncreate, read and bulk delete operations:\n\n(1) To insert documents use commands of this type:\nhttp://localhost:1880/putdata?name=graham&age=29\n\n(2) To retrieve documents:\nhttp://localhost:1880/getdata\n\n(3) To delete ALL documents:\nhttp://localhost:1880/deleteall\n\nTODO - find a more efficient way to delete all the documents.\n","x":174.3333282470703,"y":41.83332824707031,"wires":[]},{"id":"be6543f0.b00668","type":"http in","z":"b4bbf6f9.5517a","name":"Delete all documents","url":"/deleteall","method":"get","swaggerDoc":"","x":137.88333129882812,"y":282.8833312988281,"wires":[["c09671c7.b1c32"]]},{"id":"c09671c7.b1c32","type":"cloudant in","z":"b4bbf6f9.5517a","name":"Retrieve documents","cloudant":"f42e6b50.00d088","database":"mmm_test_1","service":"_ext_","search":"_all_","design":"","index":"","x":393.2166748046875,"y":282.3277282714844,"wires":[["9a63d2a.208593","3d767fa2.92db88"]]},{"id":"9a63d2a.208593","type":"http response","z":"b4bbf6f9.5517a","name":"List of documents to delete","x":697.79443359375,"y":281.9055480957031,"wires":[]},{"id":"46b4919a.629d3","type":"cloudant out","z":"b4bbf6f9.5517a","name":"Remove all documents ","cloudant":"f42e6b50.00d088","database":"mmm_test_1","service":"_ext_","payonly":false,"operation":"delete","x":699.3333740234375,"y":408.8666687011719,"wires":[]},{"id":"3d767fa2.92db88","type":"split","z":"b4bbf6f9.5517a","name":"split out individual documents","splt":"\\n","x":393.3332824707031,"y":351.2166748046875,"wires":[["78407e98.a7d348"]]},{"id":"d4e13610.96368","type":"debug","z":"b4bbf6f9.5517a","name":"id and rev to delete","active":true,"console":"false","complete":"payload","x":698.3333740234375,"y":347.5666809082031,"wires":[]},{"id":"78407e98.a7d348","type":"function","z":"b4bbf6f9.5517a","name":"Extract id and rev","func":"\nvar document = {_id: msg.payload._id, _rev: msg.payload._rev};\n\nmsg.payload = document;\n\nreturn msg;","outputs":1,"noerr":0,"x":371.3333435058594,"y":412.7833251953125,"wires":[["46b4919a.629d3","d4e13610.96368"]]},{"id":"f42e6b50.00d088","type":"cloudant","z":"","host":"myaccountname.cloudant.com","name":"My Cloudant account"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment