Over a million developers have joined DZone.

How to Trigger OpenWhisk Actions on Cloudant Databases Changes

DZone's Guide to

How to Trigger OpenWhisk Actions on Cloudant Databases Changes

Learn how to code an OpenWhisk trigger based on changes to your Cloudant database.

· Database Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

I've written about how to write JavaScript Actions for OpenWhisk on Bluemix. Actions can be triggered manually (as described in the previous article) and when certain events occur. Below is a simple sample of how to trigger OpenWhisk actions when documents in Cloudant NoSQL databases are changed or added.

Let's use the same scenario as previously. In a help desk application, you want to translate tickets that have not been written in English to English so that support agents understand. Rather than doing this when tickets are stored or opened, the OpenWhisk action is triggered when new documents are stored in a Cloudant NoSQL database.

The documentation describes how to build this scenario. Essentially you need, in addition to the JavaScript action, a trigger and a rule. Invoke the following commands via the CLI and replace your credentials, namespace, and database name.

wsk property set --auth <yourOpenWhiskUser>:<yourOpenWhiskPassword> --namespace "<yourOpenWhiskNameSpace>"
wsk package bind /whisk.system/cloudant myCloudant -p username '<yourCloudantUser>' -p password '<yourCloudantPassword>' -p host '<yourCloudantHost>'
wsk package list
wsk package get /<yourOpenWhiskNameSpace>/myCloudant
wsk trigger create myCloudantTrigger --feed /<yourOpenWhiskNameSpace>/myCloudant/changes --param <yourCloudantDBName> testdb --param includeDocs true

To see the trigger in action, create a JavaScript action 'hello.js' and a rule:

function main() {
    return {payload: 'Hello world'};

wsk action create hello hello.js
wsk rule create --enable sampleRule myCloudantTrigger hello
wsk activation poll

When you change documents or add them in your Cloudant database, you can see logs in your terminal window and the OpenWhisk dashboard.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

iot app development ,nosql ,cloudant ,openwhisk

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

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}