Search code examples
marklogic

To trigger data creation in a forest that launches a http communication in Marklogic


A few questions

  1. When creating a trigger for a given forest, is it correct to use the trgr:collection-scope function with the forest name?

https://docs.marklogic.com/trgr:trigger-data-event

  1. I'm interested in executing a jsp script that passes parameters to the script when posting a data commit to a forest. Is the following the correct way to do that?

    xquery version "1.0-ml"
    import module namespace trgr="http://marklogic.com/xdmp/triggers" 
        at "/MarkLogic/triggers.xqy";
    
    trgr:create-trigger("executeCreateBlock", "Execute Block Creation", 
    
       trgr:trigger-data-event(
       trgr:collection-scope("/str-1/"),
       trgr:document-content("create"),
       trgr:post-commit()),
    
       trgr:trigger-module(xdmp:database("str-1-modules"), "/modules/", "http.xqy"),
    
       fn:true(), 
    
       xdmp:default-permissions() 
    );
    

With the following http.xqy module that currently for testing just accepts a parameter called filename

xquery version "1.0-ml";

let $payload :=  '{ "filename": $filename}' 
return xdmp:http-post("http://chain1.xmp.com:8080/CreateBlock/response.jsp",
     <options xmlns="xdmp:http">
       <data>{$payload}</data>
     </options>)
  1. Is it possible to pass the name of the file that has been created in the database, which caused the trigger to fire, to the module script?

Thanks

Conteh


Solution

  • Have you looked at the Alerting API? It abstracts the low-level trigger details for applications that need to react to database changes on data that match (reverse) queries.

    The Alerting API automatically injects context about the document that triggered the action, via external variables. For example, your action—the code that’s called in response to an alert trigger—will include something like the following:

    xquery version "1.0-ml";
    declare namespace alert = "http://marklogic.com/xdmp/alert";
    
    declare variable $alert:config-uri as xs:string external;
    declare variable $alert:doc as node() external;
    declare variable $alert:rule as element(alert:rule) external;
    declare variable $alert:action as element(alert:action) external;
    

    As for your specific question, trgr:collecion-scope() takes the name of a collection as a parameter, usually a URI. As the name implies, it allows you to limit a trigger’s purview to a particular collection. In general, your application code shouldn’t concern itself with forests. The database does a great job of abstracting how the data is organized underneath. Your code working directly with forests will likely not be as efficient and may undermine safety checks, like duplicate URI detection.

    Start with the Alerting API.