Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmakeig/3eeae8780d308c4b81bc to your computer and use it in GitHub Desktop.
Save jmakeig/3eeae8780d308c4b81bc to your computer and use it in GitHub Desktop.
Multiple transactions in a single JavaScript statement
function tx(f, vars, opts) {
opts = opts || {};
if(!opts.isolation) { opts.isolation = "different-transaction"; }
//opts.transactionMode = "update"; // Not sure what this does here.
return xdmp.eval('(' + f.toString() + ')();', vars, opts);
}
tx(
function() {
declareUpdate();
var sem = require("/MarkLogic/semantics.xqy");
xdmp.documentInsert(sem.uuidString(), {"asdf": "asdf"});
}
);
transaction(
function() {
return fn.count(fn.collection());
}
);
function tx(f, database, modules, root) {
return function() {
var outer = arguments; // arguments closure passed into wrapped function
return xdmp.invokeFunction(
function() { return f.apply(null, outer); }, // Curry to make it arity zero
{
//database: xdmp.database(database), // TODO: Make this configurable?
transactionMode: "update-auto-commit",
isolation: "different-transaction"
}
);
}
}
// Usage:
function values(start) {
var out = [];
for(var company of cts.values(cts.jsonPropertyReference('Company'), start, ['frequency-order', 'descending', 'limit=25'])) {
out.push(
{
name: company,
count: cts.frequency(company)
}
);
}
return out;
}
// Or more transparently,
// var values = tx(function(start) {…});
// values('M');
tx(values)('M');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment