Knowledgebase:
Invoking library modules from a trigger module
25 March 2021 07:43 AM

Introduction

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.

Example

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):

TriggersExample_Triggers_Definition.xqy

3. Create a module by running below XQuery against modules database:

TriggerExample_Trigger_Module.xqy

4. Insert a library module into the modules database (minimodules):

TriggerExample_Library_Module.xqy

5. Now insert the sample document into the content database (minidb):

TriggerExample_Simple_Insert.xqy

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:

2018-04-25 11:40:50.224 Info: *****Document with /mini root /mini/test-25-1-1.xml was created.*****2018-04-25T11:40:50+05:30

NOTE: Module imports are relative to root.

References:

1. Creating and Managing Triggers With triggers.xqy - https://docs.marklogic.com/guide/app-dev/triggers

(2 vote(s))
Helpful
Not helpful

Comments (0)