Newer
Older
Lloyd Hilaiel
committed
var driver;
Lloyd Hilaiel
committed
var ready = false;
var waiting = [];
function checkReady() {
if (!ready) throw "database not ready. did you call open()?";
}
Lloyd Hilaiel
committed
// async break allow database path to be configured by calling code
Lloyd Hilaiel
committed
// a touch tricky cause client must set dbPath before releasing
Lloyd Hilaiel
committed
// control of the runloop
exports.open = function(cfg, cb) {
Lloyd Hilaiel
committed
var driverName = "sqlite";
if (cfg && cfg.driver) driverName = cfg.driver;
try {
driver = require('./db_' + driverName + '.js');
} catch(e) {
var msg = "FATAL: couldn't find database driver: " + driverName;
console.log(msg);
throw msg + ": " + e.toString();
}
driver.open(cfg, function(error) {
Lloyd Hilaiel
committed
if (error) {
Lloyd Hilaiel
committed
if (cb) cb(error);
else {
console.log("ERROR:" + error);
process.exit(1);
}
} else {
ready = true;
waiting.forEach(function(f) { f() });
waiting = [];
if (cb) cb();
Lloyd Hilaiel
committed
}
});
};
Lloyd Hilaiel
committed
exports.close = function(cb) {
Lloyd Hilaiel
committed
driver.close(function(err) {
ready = false;
cb(err);
});
};
// accepts a function that will be invoked once the database is ready for transactions.
// this hook is important to pause the rest of application startup until async database
// connection establishment is complete.
Lloyd Hilaiel
committed
exports.onReady = function(f) {
setTimeout(function() {
if (ready) f();
else waiting.push(f);
}, 0);
};
Lloyd Hilaiel
committed
[
'emailKnown',
'isStaged',
'emailsBelongToSameAccount',
'addKeyToEmail',
'stageUser',
'stageEmail',
'gotVerificationSecret',
'checkAuth',
'getSyncResponse',
'pubkeysForEmail',
'removeEmail',
'cancelAccount'
].forEach(function(fn) {
exports[fn] = function() {
checkReady();
driver[fn].apply(undefined, arguments);
};