101 lines
2.1 KiB
JavaScript
101 lines
2.1 KiB
JavaScript
'use strict';
|
|
|
|
/*global Promise*/
|
|
var PromiseA = Promise;
|
|
try {
|
|
PromiseA = require('bluebird').Promise;
|
|
} catch(e) {
|
|
console.warn("For better Promise support please use bluebird");
|
|
}
|
|
var sqlite3 = require('sqlite3');
|
|
var dbs = {};
|
|
|
|
function sanitize(str) {
|
|
return String(str).replace("'", "''");
|
|
}
|
|
|
|
function create(opts) {
|
|
var db;
|
|
|
|
if (!opts) {
|
|
opts = {};
|
|
}
|
|
|
|
if (opts.verbose) {
|
|
sqlite3.verbose();
|
|
}
|
|
|
|
if (!dbs[opts.filename]) {
|
|
dbs[opts.filename] = new sqlite3.Database(opts.filename);
|
|
}
|
|
|
|
db = dbs[opts.filename];
|
|
db.sanitize = sanitize;
|
|
db.escape = sanitize;
|
|
|
|
db.init = function (newOpts) {
|
|
if (!newOpts) {
|
|
newOpts = {};
|
|
}
|
|
|
|
var key = newOpts.key || opts.key;
|
|
var bits = newOpts.bits || opts.bits;
|
|
|
|
return new PromiseA(function (resolve, reject) {
|
|
console.log('OPTS', opts);
|
|
console.log('BITS', bits);
|
|
if (db._initialized) {
|
|
resolve(db);
|
|
return;
|
|
}
|
|
|
|
if (!key) {
|
|
if (!bits) {
|
|
db._initialized = true;
|
|
}
|
|
resolve(db);
|
|
return;
|
|
}
|
|
|
|
// TODO test key length
|
|
|
|
db._initialized = true;
|
|
db.serialize(function () {
|
|
var setup = [];
|
|
|
|
if (!bits) {
|
|
bits = 128;
|
|
}
|
|
|
|
// TODO db.run(sql, function () { resolve() });
|
|
setup.push(new PromiseA(function (resolve, reject) {
|
|
db.run("PRAGMA KEY = \"x'" + sanitize(key) + "'\"", [], function (err) {
|
|
if (err) { reject(err); return; }
|
|
resolve(this);
|
|
});
|
|
}));
|
|
setup.push(new PromiseA(function (resolve, reject) {
|
|
//process.nextTick(function () {
|
|
db.run("PRAGMA CIPHER = 'aes-" + sanitize(bits) + "-cbc'", [], function (err) {
|
|
if (err) { reject(err); return; }
|
|
resolve(this);
|
|
});
|
|
//});
|
|
}));
|
|
|
|
PromiseA.all(setup).then(function () {
|
|
// restore original functions
|
|
resolve(db);
|
|
}, reject);
|
|
});
|
|
});
|
|
};
|
|
|
|
return db.init(opts);
|
|
}
|
|
|
|
module.exports.sanitize = sanitize;
|
|
module.exports.escape = sanitize;
|
|
module.exports.Database = sqlite3.Database;
|
|
module.exports.create = create;
|