Last active
August 29, 2015 14:05
-
-
Save jmakeig/3eeae8780d308c4b81bc to your computer and use it in GitHub Desktop.
Multiple transactions in a single JavaScript statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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