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

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.

cloudant, iot app development, nosql, openwhisk

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 }}