(filenameGiven, options)
| 6 | |
| 7 | module.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 | if (Buffer.isBuffer(filenameGiven)) { |
| 16 | buffer = filenameGiven; |
| 17 | filenameGiven = ':memory:'; |
| 18 | } |
| 19 | if (filenameGiven == null) filenameGiven = ''; |
| 20 | if (options == null) options = {}; |
| 21 | |
| 22 | // Validate arguments |
| 23 | if (typeof filenameGiven !== 'string') throw new TypeError('Expected first argument to be a string'); |
| 24 | if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object'); |
| 25 | if ('readOnly' in options) throw new TypeError('Misspelled option "readOnly" should be "readonly"'); |
| 26 | if ('memory' in options) throw new TypeError('Option "memory" was removed in v7.0.0 (use ":memory:" filename instead)'); |
| 27 | |
| 28 | // Interpret options |
| 29 | const filename = filenameGiven.trim(); |
| 30 | const anonymous = filename === '' || filename === ':memory:'; |
| 31 | const readonly = util.getBooleanOption(options, 'readonly'); |
| 32 | const fileMustExist = util.getBooleanOption(options, 'fileMustExist'); |
| 33 | const timeout = 'timeout' in options ? options.timeout : 5000; |
| 34 | const verbose = 'verbose' in options ? options.verbose : null; |
| 35 | const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null; |
| 36 | |
| 37 | // Validate interpreted options |
| 38 | if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly'); |
| 39 | if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer'); |
| 40 | if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647'); |
| 41 | if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function'); |
| 42 | if (!allowNativeBinding && 'nativeBinding' in options) throw new TypeError('The "nativeBinding" option is only supported by the default better-sqlite3 entrypoint'); |
| 43 | if (allowNativeBinding && nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object'); |
| 44 | |
| 45 | // Load the native addon |
| 46 | const addon = getAddon(nativeBinding); |
| 47 | if (!addon.isInitialized) { |
| 48 | addon.initialize(SqliteError, arrayFactory, arrayAppender, rowFactory, recordFactory); |
| 49 | addon.isInitialized = true; |
| 50 | } |
| 51 | |
| 52 | // Make sure the specified directory exists |
| 53 | if (!anonymous && !filename.startsWith('file:') && !fs.existsSync(path.dirname(filename))) { |
| 54 | throw new TypeError('Cannot open database because the directory does not exist'); |
| 55 | } |
| 56 | |
| 57 | Object.defineProperties(this, { |
| 58 | [util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) }, |
| 59 | ...wrappers.getters, |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | const wrappers = require('./methods/wrappers'); |
| 64 | Database.prototype.prepare = wrappers.prepare; |
no outgoing calls
no test coverage detected