From 8f7441dd8f3c53ea5eaa6c6e955844fb6bc444d5 Mon Sep 17 00:00:00 2001 From: destyk Date: Tue, 28 Mar 2023 21:20:13 +0800 Subject: [PATCH 1/5] Added `webpack` support --- lib/database.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/database.js b/lib/database.js index aea774d37..6b26cf5ae 100644 --- a/lib/database.js +++ b/lib/database.js @@ -47,8 +47,15 @@ function Database(filenameGiven, options) { if (nativeBindingPath == null) { addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node')); } else { - addon = require(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node')); + // If webpack is used to build the application, we must use `__non_webpack_require__`. + const requireFunc = + typeof __webpack_require__ === 'function' + ? __non_webpack_require__ + : require; + + addon = requireFunc(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node')); } + if (!addon.isInitialized) { addon.setErrorConstructor(SqliteError); addon.isInitialized = true; From 886a913aff76dc019baa0615c34c35e7dc21c970 Mon Sep 17 00:00:00 2001 From: destyk Date: Wed, 12 Apr 2023 18:23:45 +0800 Subject: [PATCH 2/5] changed from `webpack_require` to `non_webpack_require` --- lib/database.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/database.js b/lib/database.js index 6b26cf5ae..38bf77aa6 100644 --- a/lib/database.js +++ b/lib/database.js @@ -49,9 +49,7 @@ function Database(filenameGiven, options) { } else { // If webpack is used to build the application, we must use `__non_webpack_require__`. const requireFunc = - typeof __webpack_require__ === 'function' - ? __non_webpack_require__ - : require; + typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require; addon = requireFunc(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node')); } From abcb06833b6ea7690b5db5830b09eb45a9609be0 Mon Sep 17 00:00:00 2001 From: Matthew McEachen Date: Tue, 18 Apr 2023 17:45:09 -0700 Subject: [PATCH 3/5] Update database.js Added URL to webpack documentation --- lib/database.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/database.js b/lib/database.js index 38bf77aa6..f0389d61d 100644 --- a/lib/database.js +++ b/lib/database.js @@ -48,6 +48,7 @@ function Database(filenameGiven, options) { addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node')); } else { // If webpack is used to build the application, we must use `__non_webpack_require__`. + // See const requireFunc = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require; From a3582b2dfdf613086c90edd9ed75a44e6bb9bb9d Mon Sep 17 00:00:00 2001 From: destyk Date: Mon, 19 Jun 2023 16:30:25 +0800 Subject: [PATCH 4/5] feat(database): now `nativeBinding` supports addon object --- lib/database.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/database.js b/lib/database.js index f0389d61d..80abd5d89 100644 --- a/lib/database.js +++ b/lib/database.js @@ -33,26 +33,43 @@ function Database(filenameGiven, options) { const fileMustExist = util.getBooleanOption(options, 'fileMustExist'); const timeout = 'timeout' in options ? options.timeout : 5000; const verbose = 'verbose' in options ? options.verbose : null; - const nativeBindingPath = 'nativeBinding' in options ? options.nativeBinding : null; + const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null; // Validate interpreted options if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly'); if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer'); if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647'); if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function'); - if (nativeBindingPath != null && typeof nativeBindingPath !== 'string') throw new TypeError('Expected the "nativeBinding" option to be a string'); + if (nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object'); // Load the native addon let addon; - if (nativeBindingPath == null) { - addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node')); - } else { - // If webpack is used to build the application, we must use `__non_webpack_require__`. - // See - const requireFunc = - typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require; + switch(true) { + case nativeBinding == null: { + addon = DEFAULT_ADDON || (DEFAULT_ADDON = require('bindings')('better_sqlite3.node')); + break; + } - addon = requireFunc(path.resolve(nativeBindingPath).replace(/(\.node)?$/, '.node')); + case typeof nativeBinding === 'string': { + // If webpack is used to build the application, we must use `__non_webpack_require__`. + // See + const requireFunc = + typeof __non_webpack_require__ === 'function' ? __non_webpack_require__ : require; + + addon = requireFunc(path.resolve(nativeBinding).replace(/(\.node)?$/, '.node')); + break; + } + + case typeof nativeBinding === 'object': { + // If `nativeBinding` is addon object. + // See + addon = nativeBinding; + break; + } + + default: { + throw new TypeError('nativeBinding has an invalid value passed'); + } } if (!addon.isInitialized) { From 4b22aa63196649d85d7b96c7beb83dba276750b7 Mon Sep 17 00:00:00 2001 From: destyk Date: Mon, 19 Jun 2023 16:30:57 +0800 Subject: [PATCH 5/5] style(package): line break removed --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 50c7757d2..491bac2a8 100644 --- a/package.json +++ b/package.json @@ -57,4 +57,4 @@ "window functions", "database" ] -} +} \ No newline at end of file