Skip to content

Commit b409125

Browse files
committed
Add option to set the default transaction behavior
Fixes WiseLibs#1217
1 parent 7896456 commit b409125

9 files changed

+3246
-3206
lines changed

docs/api.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Various options are accepted:
3535

3636
- `options.timeout`: the number of milliseconds to wait when executing queries on a locked database, before throwing a `SQLITE_BUSY` error (default: `5000`).
3737

38+
- `options.defaultTransactionBehaviour`: Set the default transaction behavior when calling [Database#transaction()](#transactionfunction---function). Can be set as `null` (SQLite Default), `'deferred'`, `'immediate'` or `'exclusive'` (default: `null`).
39+
3840
- `options.verbose`: provide a function that gets called with every SQL string executed by the database connection (default: `null`).
3941

4042
- `options.nativeBinding`: if you're using a complicated build system that moves, transforms, or concatenates your JS files, `better-sqlite3` might have trouble locating its native C++ addon (`better_sqlite3.node`). If you get an error that looks like [this](https://github.com/JoshuaWise/better-sqlite3/issues/534#issuecomment-757907190), you can solve it by using this option to provide the file path of `better_sqlite3.node` (relative to the current working directory).
@@ -86,7 +88,8 @@ const adopt = db.transaction((cats) => {
8688
Transactions also come with `deferred`, `immediate`, and `exclusive` versions.
8789

8890
```js
89-
insertMany(cats); // uses "BEGIN"
91+
insertMany(cats) // uses the default set by options.defaultTransactionBehavior
92+
insertMany.default(cats); // uses "BEGIN"
9093
insertMany.deferred(cats); // uses "BEGIN DEFERRED"
9194
insertMany.immediate(cats); // uses "BEGIN IMMEDIATE"
9295
insertMany.exclusive(cats); // uses "BEGIN EXCLUSIVE"

lib/database.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ function Database(filenameGiven, options) {
3434
const timeout = 'timeout' in options ? options.timeout : 5000;
3535
const verbose = 'verbose' in options ? options.verbose : null;
3636
const nativeBinding = 'nativeBinding' in options ? options.nativeBinding : null;
37+
const defaultTransactionBehavior = 'defaultTransactionBehavior' in options ? options.defaultTransactionBehavior : null;
3738

3839
// Validate interpreted options
3940
if (readonly && anonymous && !buffer) throw new TypeError('In-memory/temporary databases cannot be readonly');
4041
if (!Number.isInteger(timeout) || timeout < 0) throw new TypeError('Expected the "timeout" option to be a positive integer');
4142
if (timeout > 0x7fffffff) throw new RangeError('Option "timeout" cannot be greater than 2147483647');
4243
if (verbose != null && typeof verbose !== 'function') throw new TypeError('Expected the "verbose" option to be a function');
4344
if (nativeBinding != null && typeof nativeBinding !== 'string' && typeof nativeBinding !== 'object') throw new TypeError('Expected the "nativeBinding" option to be a string or addon object');
45+
if (defaultTransactionBehavior != null) {
46+
if (typeof defaultTransactionBehavior !== 'string') throw new TypeError('Expected the "defaultTransactionBehavior" option to be a string, expected "deferred", "immediate", or "exclusive"')
47+
if (!["deferred", "immediate", "exclusive"].includes(defaultTransactionBehavior)) throw new Error('Invalid value for "defaultTransactionBehavior", expected "deferred", "immediate", or "exclusive"')
48+
}
4449

4550
// Load the native addon
4651
let addon;
@@ -66,7 +71,7 @@ function Database(filenameGiven, options) {
6671
}
6772

6873
Object.defineProperties(this, {
69-
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, verbose || null, buffer || null) },
74+
[util.cppdb]: { value: new addon.Database(filename, filenameGiven, anonymous, readonly, fileMustExist, timeout, defaultTransactionBehavior, verbose || null, buffer || null) },
7075
...wrappers.getters,
7176
});
7277
}

lib/methods/transaction.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ module.exports = function transaction(fn) {
1111

1212
// Each version of the transaction function has these same properties
1313
const properties = {
14-
default: { value: wrapTransaction(apply, fn, db, controller.default) },
1514
deferred: { value: wrapTransaction(apply, fn, db, controller.deferred) },
1615
immediate: { value: wrapTransaction(apply, fn, db, controller.immediate) },
1716
exclusive: { value: wrapTransaction(apply, fn, db, controller.exclusive) },
1817
database: { value: this, enumerable: true },
1918
};
19+
properties.default = db.defaultTransactionBehavior == null ?
20+
{ value: wrapTransaction(apply, fn, db, controller.default) } :
21+
properties[db.defaultTransactionBehavior];
2022

2123
Object.defineProperties(properties.default.value, properties);
2224
Object.defineProperties(properties.deferred.value, properties);

lib/methods/wrappers.js

+4
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ exports.getters = {
5151
get: function memory() { return this[cppdb].memory; },
5252
enumerable: true,
5353
},
54+
defaultTransactionBehavior: {
55+
get: function defaultTransactionBehavior() { return this[cppdb].defaultTransactionBehavior; },
56+
enumerable: true
57+
}
5458
};

0 commit comments

Comments
 (0)