Skip to content

JavaScript Modules

Mads Hansen edited this page Jan 21, 2021 · 9 revisions

JavaScript modules are supported with Marklogic 8 or greater and can be used in place of an XQuery module. However, if returning multiple values (ex: URIS-MODULE), values must be returned as a ValueIterator. MarkLogic JavaScript API has helper functions to convert Arrays into ValueIterator: xdmp.arrayValues() and inserting values into another ValueIterator: fn.insertBefore().

JavaScript modules must have an .sjs file extension when deployed to a Modules database. However, adhoc JavaScript modules support both .sjs and .js file extensions.

For example, a simple URIS-MODULE may look like this:

let uris = cts.uris();
fn.insertBefore(uris,0,fn.count(uris));

To return URIS_BATCH_REF, we can do the following:

let uris = cts.uris();
fn.insertBefore(fn.insertBefore(uris,0,fn.count(uris)),0,"batch-ref");

Note: Do not use single quotes within (adhoc) JavaScript modules. If you must use a single quote, escape it with a quote (ex: ''text'')

For the PROCESS-MODULE, declare a global variable named URI:

var URI;
"The URI is: " + URI;