Skip to content Skip to sidebar Skip to footer

Javascript Multi-statement Transaction In Marklogic

I wanted to write a multi-statement transaction in server-side JavaScript in marklogic. What I wanted to achieve is, do an update transaction and then write a query statement which

Solution 1:

In both SJS and XQuery, the approach to inspect the result of a transaction is to eval both the transaction and the inspection in statements that are different from the choreographing outer statement.

(The XQuery semicolon syntax separates statements that execute in different transactions -- the equivalent of a series of evals without a choreographing outer statement.)

Something similar to the following should work:

'use strict';
xdmp.eval(
    'declareUpdate(); xdmp.documentInsert("/docs/first.json",{"first": 1});',
    null,
    {isolation:'different-transaction'});
const doc = xdmp.eval(
    'cts.doc("/docs/first.json")',
    null,
    {isolation:'different-transaction'});
fn.exists(doc);

That said, it's unnecessary to verify document insertion. If the insertion fails, the server throws an error.

It's also unnecessary to use a different transaction to read the inserted document in order to return it. Just return the document after the xdmp.insert() call.

Hoping that helps,

Post a Comment for "Javascript Multi-statement Transaction In Marklogic"