Jump to content

XQuery/Using Triggers to Log Events

From Wikibooks, open books for an open world

Motivation

[edit | edit source]

You want to log all changes in files of a single collection.

Method

[edit | edit source]

We will create a trigger that logs these events. The trigger will append a string to a log file.

There are six trigger event types:

  • store: Fired when a document is created in the collection or sub-collection
  • update: Fired when a document is updated in the collection or sub-collection
  • remove: Fired when a document is deleted from the collection or sub-collection
  • create: Fired when a sub-collection is created
  • rename: Fired when a sub-collection is renamed
  • delete: Fired when a sub-collection is deleted

Sample Code

[edit | edit source]

NOTE: These examples do not reliably work on eXist 1.4! I have not yet tested on 2.0.

In this example we will be logging all store, update and remove events from the collection /db/my-collection

Here is a sample trigger configuration file. This file is placed in the /db/system/config are with the same db path added to it that you want to monitor:

  /db/system/config/db/my-collection

Here is what the trigger file looks like:

collection.xconf

<collection xmlns="http://exist-db.org/collection-config/1.0">
  <triggers>
    <trigger event="store, update, remove, create, rename, delete"
            class="org.exist.collections.triggers.XQueryTrigger">
       <parameter name="url"
           value="xmldb:exist://localhost/db/triggers/log-changes.xq"/>
       <parameter name="test" value="test-value"/>
    </trigger>
  </triggers>
</collection>

Note that the three trigger operations (store, update, remove) are listed in the event attribute and separated by commas. When these operations are fired the XQuery /db/triggers/log-changes.xq gets run. You can pass parameters to this query using the parameter element.

XQuery logger

[edit | edit source]
xquery version "1.0";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace response="http://exist-db.org/xquery/response";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace util="http://exist-db.org/xquery/util";

declare variable $local:triggerEvent external;
declare variable $local:eventType external;
declare variable $local:collectionName external;
declare variable $local:documentName external;
declare variable $local:document external;
declare variable $local:test external;
declare variable $local:triggersLogFile := "triggersLog.xml";

(: create the log file if it does not exist :)
if(not(doc-available($local:triggersLogFile)))
   then ( xmldb:store("/db", $local:triggersLogFile, <events/>) ) 
   else(),

update
   insert
      <event ts="{ current-dateTime() }"
        event="{$local:triggerEvent}"
        eventType="{$local:eventType}"
        test-1="{$local:test}"
        collectionName="{$local:collectionName}"
        documentName="{$local:documentName}" >
        {$local:document}
      </event>
    into doc(concat("/db/", $local:triggersLogFile))/events

References

[edit | edit source]

Guide to configuring eXist triggers