0
votes

MarkLogic version : 9.0-6.2

All documents in our DB are JSONs and we use Javascript to manipulate data. I am trying to create a pipeline that identifies whenever a Phone Number in the JSON document is updated (using a condition module) and then runs an action module. So far, I got the below code.

{
  "pipeline-name": "CPF async Test",
  "pipeline-description": "CPF Test",
  "success-action": {
      "module": "/MarkLogic/cpf/actions/success-action.xqy"
  },
  "failure-action": {
      "module": "/marklogic/cpf/actions/failure-action.xqy"
  },
  "status-transition": [
    {
      "annotation": "",
      "status": "updated",
      "on-success": "http://marklogic.com/states/done",
      "on-failure": "http://marklogic.com/states/error",
      "execute": [
         {
           "condition": {
           "module": ???
           },
           "action": {
           "module": "sleepTest.sjs"
          }
        }
     ]
   }
 ]
}
  1. When I tried to load this JSON as pipeline using admin console (with filter as filename.json), I am getting a message "Invalid input: No readable XML files found:"

    Should a pipeline be always XML?

  2. I am able to write (in JavaScript) and execute the action module successfully. I tested it within an XML pipeline. Can I write a condition module in JavaScript too?

  3. I am not sure if there is a built-in condition module provided by MarkLogic that identifies the changes to a property (by passing the property as parameter to the condition module). If yes, could you please point to the documentation? If I have to create a custom condition module, how can I pass old and new phone numbers to the module? How would the condition module look like, preferably in JavaScript?

Most of the documentation I found on CPF is XML/XQuery. Any pointers to JSON/JavaScript CPF documentation is appreciated.

Thanks in advance!

2

2 Answers

0
votes

The example that @rjrudin pointed out only have CPF condition module in XQuery. Below is a comparison of the XQuery and the Server-side JavaScript versions (logging omitted):

sample-condition.xqy link to the original on GitHub

xquery version "1.0-ml";
declare namespace cpf = "http://marklogic.com/cpf";
declare variable $cpf:document-uri as xs:string external;
(: your custom condition logic :)
return true()  (: or false() :)

sample-condition.sjs

'use strict'
const cpf = require('/MarkLogic/cpf/cpf')
// your custom condition logic
fn.true()  // or fn.false()

Documentation: https://docs.marklogic.com/js/cpf

0
votes

Pipelines can be in XML or JSON - there are some examples in this ml-gradle sample project - https://github.com/marklogic-community/ml-gradle/tree/master/examples/cpf-project/src/main/ml-config/cpf/pipelines .

I think condition modules can be either JS or XQuery.

As for detecting the change - CPF runs via post-commit triggers, so the document will have already been updated by the time the condition or action module runs. Thus, you need to store the old value somewhere on the document. This is tangential to MarkLogic, but one approach here is to use the JSON Patch spec so that clients specify changes via patches. You can persist both the patch as a separate document (or as part of the phone number document) and update the phone number document. The condition/action module can then look at the latest patch to see what change was made.