Publishing Changes Downstream

To publish changes to downstream systems the Custodian product relies upon the monsterDB to be able to push messages outbound, this is because monsterDB is the lowest level common denominator that can be used to publish changes for all updates into the custodian database. For example, if a system was to update a record in a custodian domain directly in the monsterDB database we would still want those changes to be published to downstream.

Setting up a handler

To setup a publishing module for a database (akin to a project in Custodian) you would first need to write a event handler in Javascript in a .js file on the local machine where you access monsterDB. Although the command to setup the event handler can be run via the monsterDB terminal in the custodian application, the file would need to be created directly on the server running monsterDB.

If you are running monsterDB in a distributed way, then you would need to login to a single node in the cluster and have the .js file available locally to that machine. The system will distribute the .js event handler code around all the nodes in the cluster and so this only needs to be done once and again after any change to the js code.

First start a monsterDB terminal and connect to the database corresponding to the project, the database naming convention for Custodian projects is [machine]-[project] where machine is the name of the instance you are running, if you are unsure this information is available from the User Profile option on the settings menu (COG):

As indicated the instance name should be used as the machine name, if you are in doubt then you can use the command listDatabases in monster to list the database names.

To install the js file into the database use the following command:

use custodia-demo
db setScriptPath /pathToJSFile/trigger.js

Of course the contents of the js file are important, but we will address this later in the section.

To assign the trigger to a collection (domain) you can use addTrigger as follows where we install the trigger onto a collection called NODES which must be within the same database as the installed script:

db.NODES.addTrigger(“myTrigger”)

The value “myTrigger” respresents a method name inside the js event file installed above. It can be any valid JS method name.

A trigger can be applied to any number of collections (domains) in a single database.

JS Event Handler Code

Methods that can be used in the JS Event Code file:

init(database) – will be called at startup or initialization of the event handler in a database. You should cache the reference to the database that will be passed as this can be used in the JS code to talk to the database, it will only ever be passed once!

method(action, data) – this method can be any valid method name except init, and this method will be called on each event, asynchronously after event
“action” is “SAVE”, “MERGE”, “LINK”, or “EID”
“data” is a javascript object and for each action it will contain:

  • SAVE – an object representing record only
  • MERGE – an object with left, right, match and golden documents
  • LINK – an object with left, right and match documents
  • EID – an object with left, right and match documents

Example

var db;
function init(database){
    db=database;
    print("User code is listening to:" + db.getName());
}
function triggerIt(action, data){
     var json=JSON.parse(data);
     if (action=="SAVE"){
       var first=db.getCollection("NODES").find(new com.entitystream.monster.db.Document("_id",json["_id"])).first();
       print("Saved database image is " + first.toJson());
     } else
     if (action=="MERGE"){
       var left=json["left"];
       var right=json["right"];
       var match=json["match"];
       var result=json["result"];
       print (left["_id"] + " merged with " + right["_id"] + " with a score of " + match["score"] + " resulting in a golden record: " + result);
     } else
     if (action=="LINK"){
       var left=json["left"];
       var right=json["right"];
       var match=json["match"];
       print (left["_id"] + " linked with " + right["_id"] + " with a score of " + match["score"] + " with the verb" + match["linkText"]);
     } else
     if (action=="EID"){
       var left=json["left"];
       var right=json["right"];
       var match=json["match"];
       print (left["_id"] + " assigned same eid to " + right["_id"] + " with a score of " + match["score"] + " EID " + left["EID"]);
     }
 }