Skip to content

Instantly share code, notes, and snippets.

@fgeorges
Created February 21, 2016 13:50
Show Gist options
  • Save fgeorges/606b45caf8b368279f92 to your computer and use it in GitHub Desktop.
Save fgeorges/606b45caf8b368279f92 to your computer and use it in GitHub Desktop.
Add a trigger with MarkLogic
Log excerpt after creation of a new document "/test/hello-world.xml":
2015-10-29 16:18:39.687 Info: expath-console: ***** Document /test/hello-world.xml was created. *****
2015-10-29 16:18:39.687 Info: expath-console: <?xml version="1.0" encoding="UTF-8"?>
2015-10-29 16:18:39.687 Info: expath-console: <hello>World!</hello>
2015-10-29 16:18:39.687 Info: expath-console: *****
(: This is the trigger itself, just logging a document's URI and content. :)
(: To be put in "/test/log.xqy" in the Triggers database. :)
import module namespace tr = "http://marklogic.com/xdmp/triggers"
at "/MarkLogic/triggers.xqy";
(: this variable will be bound by MarkLogic when invoking the trigger :)
declare variable $tr:uri as xs:string external;
xdmp:log(
'***** Document '
|| $tr:uri
|| ' was created. *****&#10;'
|| xdmp:quote(fn:doc($tr:uri))
|| '&#10;*****')
(: Set up the trigger (to be evaluated against Triggers DB, e.g. on QConsole). :)
import module namespace tr = "http://marklogic.com/xdmp/triggers"
at "/MarkLogic/triggers.xqy";
(: to change only if the trigger is not named '/test/log.xqy', on 'Triggers' :)
declare variable $trigger-db := 'Triggers';
declare variable $trigger-dir := '/test/';
declare variable $trigger-name := 'log.xqy';
(: install the trigger to log all document creation in /test/* :)
tr:create-trigger(
'log-test',
'Simple test trigger, logging doc creations in /test/.',
tr:trigger-data-event(
(: "listens" to /test/* (direct children), on Documents :)
tr:directory-scope('/test/', '1'),
(: "listens" to document creations :)
tr:document-content('create'),
(: no comment... :)
tr:pre-commit()),
(: /test/log.xqy on Triggers (or whatever is in the variables) :)
tr:trigger-module(
xdmp:database($trigger-db),
$trigger-dir,
$trigger-name),
fn:true(),
xdmp:default-permissions())
@fgeorges
Copy link
Author

Files seem to be re-ordered alphabetically by name. This Gist is a simple example of a trigger and how to set it up on MarkLogic.

  • Look at log.xqy first, which is the trigger itself (to be in the trigger database).
  • Then setup.xqy which installs the trigger (which has to be evaluated, e.g. from QConsole or from the EXPath Console).
  • The file log-excerpt.txt is a small excerpt of the log file showing the result when a new document is created on the database, and the trigger has then been invoked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment