MCPcopy Create free account
hub / github.com/WiseLibs/better-sqlite3 / Database

Function Database

lib/database.js:8–72  ·  view source on GitHub ↗
(filenameGiven, options)

Source from the content-addressed store, hash-verified

6
7module.exports = function createDatabase(getAddon, allowNativeBinding) {
8 function Database(filenameGiven, options) {
9 if (new.target == null) {
10 return new Database(filenameGiven, options);
11 }
12
13 // Apply defaults
14 let buffer;
15 let fd;
16 if (Buffer.isBuffer(filenameGiven)) {
17 buffer = filenameGiven;
18 filenameGiven = ':memory:';
19 } else if (typeof filenameGiven === 'number') {
20 if (!Number.isInteger(filenameGiven) || filenameGiven < 0 || filenameGiven > 0x7fffffff) {
21 throw new TypeError('Expected the file descriptor to be a non-negative 32-bit integer');
22 }
23 fd = filenameGiven;
24 filenameGiven = `fd:${fd}`;
25 }
26 if (filenameGiven == null) filenameGiven = '';
27 if (options == null) options = {};
28
29 // Validate arguments
30 if (typeof filenameGiven !== 'string') throw new TypeError('Expected first argument to be a string');
31 if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
32 if ('readOnly' in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"');
33 if ('memory' in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)');
34
35 // Interpret options
36 const filename = filenameGiven.trim();
37 const anonymous = filename === '' || filename === ':memory:';
38 const readonly = util.getBooleanOption(options, 'readonly');
39 const fileMustExist = util.getBooleanOption(options, 'fileMustExist');
40 const timeout = 'timeout' in options ? options.timeout : 5000;
41 const verbose = 'verbose' in options ? options.verbose : null;
42 const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;
43 const journalFd = 'journalFd' in options ? options.journalFd : undefined;
44
45 // Validate interpreted options
46 if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
47 if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
48 if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
49 if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
50 if (fd !== undefined && journalFd === undefined) throw new TypeError('The "journalFd" option is required when opening a database from a file descriptor');
51 if (fd === undefined && journalFd !== undefined) throw new TypeError('The "journalFd" option can only be used when opening a database from a file descriptor');
52 if (journalFd !== undefined && !(Number.isInteger(journalFd) && journalFd >= 0 && journalFd <= 0x7fffffff)) throw new TypeError('Expected the "journalFd" option to be a non-negative 32-bit integer');
53 if (!allowNativeBinding && 'nativeBinding' in options) throw new TypeError('The "nativeBinding" option is only supported by the default better-sqlite3 entrypoint');
54 if (allowNativeBinding && nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
55
56 // Load the native addon
57 const addon = getAddon(nativeBinding);
58 if (!addon.isInitialized) {
59 addon.initialize(SqliteError, arrayFactory, arrayAppender, rowFactory, recordFactory);
60 addon.isInitialized = true;
61 }
62
63 // Make sure the specified directory exists
64 if (fd === undefined && !anonymous && !filename.startsWith('file:') && !fs.existsSync(path.dirname(filename))) {
65 throw new TypeError('Cannot open database because the directory does not exist');

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected