Invoking library modules from a trigger module
25 March 2021 07:43 AM
MarkLogic Server provides pre-commit or post-commit triggers and these triggers listen for certain events to occur and then invokes a configured XQuery module to run after the event occurs. It is a common use case to create a common function in a library module which is shared among different trigger modules called by various triggers. This article shows an example to create and use such a shared library module in a post-commit trigger.
This example shows a simple post commit trigger that fires when a new document is created.
1. For this example create a database 'minidb' and after that set its triggers database as self (minidb). Also, create another database 'minimodule' to store all modules.
2. Using Query Console, create a trigger using trigger definition by evaluating below XQuery against triggers database (minidb):
3. Create a module by running below XQuery against modules database:
4. Insert a library module into the modules database (minimodules):
5. Now insert the sample document into the content database (minidb):
6. Check the output in logs:
After a new document having its URI prefixed with "/mini" is inserted into the content database, the TaskServer Logs file logs the below message:
NOTE: Module imports are relative to root.
1. Creating and Managing Triggers With triggers.xqy - https://docs.marklogic.com/guide/app-dev/triggers