mirror of
https://github.com/hanzoai/better-sqlite3-multiple-ciphers.git
synced 2026-09-24 16:57:19 +00:00
56 lines
2.0 KiB
JavaScript
56 lines
2.0 KiB
JavaScript
'use strict';
|
|
var path = require('path');
|
|
var toDescriptor = require('to-descriptor');
|
|
var util = require('./util');
|
|
var CPPDatabase = require('bindings')({
|
|
bindings: 'better_sqlite3.node',
|
|
module_root: path.resolve(__dirname, '..'),
|
|
}).Database;
|
|
|
|
function Database(filenameGiven, options) {
|
|
if (typeof filenameGiven !== 'string') {
|
|
throw new TypeError('Expected argument 0 to be a string filename');
|
|
}
|
|
var filename = filenameGiven.trim();
|
|
if (!filename) {
|
|
throw new TypeError('A database filename cannot be an empty string');
|
|
}
|
|
if (/^file:/i.test(filename)) {
|
|
throw new TypeError('URI filenames are reserved for internal use only');
|
|
}
|
|
if (/^(:memory:)?$/i.test(filename)) {
|
|
throw new TypeError('To create an in-memory database, specify a normal filename and use the "memory" option');
|
|
}
|
|
|
|
if (typeof options !== 'object' || options === null) {
|
|
options = {};
|
|
}
|
|
var memory = util.getBooleanOption(options, 'memory');
|
|
var readonly = util.getBooleanOption(options, 'readonly');
|
|
|
|
if (memory) {
|
|
if (process.platform === 'win32') {
|
|
filename = filename.replace(/\\/g, '/').replace(/^[a-z]:\//i, '/$&');
|
|
}
|
|
filename = 'file:'
|
|
+ filename
|
|
.replace(/\?/g, '%3f')
|
|
.replace(/\/\/+/g, '/')
|
|
.replace(/#/g, '%23')
|
|
+ '?mode=memory&cache=shared';
|
|
} else if (!util.pathExists(path.dirname(filename))) {
|
|
throw new TypeError('Cannot open database because the directory does not exist');
|
|
}
|
|
|
|
return new CPPDatabase(filename, filenameGiven, memory, readonly);
|
|
}
|
|
|
|
CPPDatabase.prototype.checkpoint = require('./checkpoint')(CPPDatabase.prototype.checkpoint);
|
|
CPPDatabase.prototype.pragma = require('./pragma')(CPPDatabase.prototype.pragma);
|
|
CPPDatabase.prototype.register = require('./register')(CPPDatabase.prototype.register);
|
|
CPPDatabase.prototype.constructor = Database;
|
|
Database.prototype = Object.create(Object.prototype, toDescriptor(CPPDatabase.prototype));
|
|
Object.keys(CPPDatabase.prototype).forEach(function (method) {delete CPPDatabase.prototype[method];});
|
|
Object.setPrototypeOf(CPPDatabase.prototype, Database.prototype);
|
|
module.exports = Database;
|