Skip to content

Instantly share code, notes, and snippets.

@adamfowleruk
Last active December 15, 2015 10:19
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 adamfowleruk/5244601 to your computer and use it in GitHub Desktop.
Save adamfowleruk/5244601 to your computer and use it in GitHub Desktop.
MarkLogic World 2013 - MLDB talk
var dosomething = function() { /* whatever */ };
var db = new mldb(); // default options
db.exists(function(result) {
if (!result.inError) {
if (result.exists) {
// already exists
dosomething();
} else {
// create then destroy
db.create(function(result) {
dosomething();
});
}
}
});
var defaultdboptions = {
host: "localhost", port: 9090, adminport: 8002, ssl: false,
auth: "digest", username: "admin",password: "admin",
database: "mldbtest",
searchoptions: {}, fastthreads: 10, fastparts: 100
};
var db = new mldb(); // default options
db.setLogger(logger);
var doc = db.get("/messages/1", function(result) {
// now print it
logger.debug("Doc content: " + JSON.stringify(result.doc));
// done
});
var db = new mldb(); // default options
db.save({from: "test", to: "all", body: "wibble"},"/messages/1",
{collection: "messages"},function(result) {
// now fetch it
db.get("/messages/1", function(result) {
// now print it
logger.debug("Doc content: " + JSON.stringify(result.doc));
// now delete it
db.delete("/messages/1", function(result) {
// done
});
});
});
tests.commit = function(callback) {
var db = new mldb(); // default options
db.setLogger(logger);
db.beginTransaction(function(result) {
var txid = result.txid;
logger.debug("TEST: COMMIT: Transaction ID: " + txid);
// now create doc
var uri = "/trans/commit/1";
var json = {title: "Transaction commit test doc"};
db.save(json,uri,function(result) {
// now commit
db.commitTransaction(function(result) {
if (result.inError) {
logger.debug("TEST: COMMIT: ERROR IN COMMIT: " + JSON.stringify(result));
callback(false);
} else {
db.get(uri, function(result) {
// now print it
logger.debug("TEST: COMMIT: Doc content: " + JSON.stringify(result.doc));
if (JSON.stringify(json) == JSON.stringify(result.doc)) {
// now delete it
db.delete(uri, function(result) {
logger.debug("TEST: COMMIT: Doc deleted");
});
} else {
logger.debug("TEST: COMMIT: ERROR: Retrieved document is not same as that saved");
callback(false);
}
});
}
});
});
});
};
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment