@aria-framework/db-worker 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/driver/sqlite.js +100 -29
- package/package.json +2 -2
package/driver/sqlite.js
CHANGED
|
@@ -67,17 +67,89 @@ function createSqliteDriver(opts = {}) {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/** The contract over an open better-sqlite3 handle. */
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
|
|
70
|
+
// NESTING IS A PROPERTY OF THE CALL STACK, NOT OF THE CONNECTION.
|
|
71
|
+
//
|
|
72
|
+
// This was a depth counter hung off the handle, and a counter cannot tell two different things
|
|
73
|
+
// apart, because both look like "a transaction began while a transaction was open":
|
|
74
|
+
//
|
|
75
|
+
// NESTED transaction() called INSIDE another's callback -> a savepoint
|
|
76
|
+
// OVERLAP transaction() called WHILE another is open -> must wait its turn
|
|
77
|
+
//
|
|
78
|
+
// Treating overlap as nesting corrupted both sides: the second got a savepoint, the first's
|
|
79
|
+
// COMMIT destroyed every savepoint on the connection, and the second was then REJECTED while
|
|
80
|
+
// its writes stayed committed — a rollback that did not roll back. AsyncLocalStorage answers
|
|
81
|
+
// the question a counter cannot, because it follows the async call chain, which is what
|
|
82
|
+
// "inside" actually means.
|
|
83
|
+
//
|
|
84
|
+
// The context names the connection it belongs to. That is what the old per-connection counter
|
|
85
|
+
// was really protecting: a legacy { getDb } shim mints its OWN driver over a shared handle
|
|
86
|
+
// (auth/resolveDriver.js), and both must agree about nesting — while two different databases
|
|
87
|
+
// stay independent of each other.
|
|
88
|
+
const { AsyncLocalStorage } = require('node:async_hooks');
|
|
89
|
+
const txContext = new AsyncLocalStorage();
|
|
90
|
+
|
|
91
|
+
/** Per-connection serialisation of TOP-LEVEL transactions. Hung off the handle for the same
|
|
92
|
+
* reason the depth was: every wrapper over one connection has to share one queue. */
|
|
93
|
+
const QUEUE_OWNER = Symbol.for('aria.db-worker.txqueue');
|
|
77
94
|
|
|
78
95
|
function wrap(db) {
|
|
79
96
|
const stmt = (sql) => db.prepare(sql);
|
|
80
|
-
|
|
97
|
+
// The connection is the owner of the top-level queue, and it has to BE an owner object rather
|
|
98
|
+
// than a bare promise so enqueue() can treat it exactly like any other level.
|
|
99
|
+
if (!db[QUEUE_OWNER]) db[QUEUE_OWNER] = { queue: Promise.resolve() };
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* One transaction context per LEVEL, each owning the queue for its own children.
|
|
103
|
+
*
|
|
104
|
+
* A single queue on the connection is not enough: two nested transactions started together
|
|
105
|
+
* inside one parent (a Promise.all over two stores, say) open two savepoints at once, and
|
|
106
|
+
* releasing the outer of the two releases the inner with it — the original bug, one level
|
|
107
|
+
* down. Siblings therefore take turns, exactly as top-level transactions do.
|
|
108
|
+
*
|
|
109
|
+
* Depth is fixed when the context is made rather than incremented and decremented in place,
|
|
110
|
+
* so a sibling cannot observe a depth that belongs to another one.
|
|
111
|
+
*/
|
|
112
|
+
const context = (depth) => ({ db, depth, queue: Promise.resolve() });
|
|
113
|
+
|
|
114
|
+
/** Serialise a child behind its siblings. Chained on BOTH settle paths, so one failure does
|
|
115
|
+
* not wedge every transaction queued after it. */
|
|
116
|
+
function enqueue(owner, run) {
|
|
117
|
+
const mine = owner.queue.then(run, run);
|
|
118
|
+
owner.queue = mine.then(() => {}, () => {});
|
|
119
|
+
return mine;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A real transaction. Entered only with the connection to itself. */
|
|
123
|
+
async function top(fn) {
|
|
124
|
+
const ctx = context(0);
|
|
125
|
+
db.exec('BEGIN');
|
|
126
|
+
try {
|
|
127
|
+
const out = await txContext.run(ctx, () => fn(api));
|
|
128
|
+
db.exec('COMMIT');
|
|
129
|
+
return out;
|
|
130
|
+
} catch (e) {
|
|
131
|
+
// The original error is what matters; a failed rollback must not mask it.
|
|
132
|
+
try { db.exec('ROLLBACK'); } catch (_) { /* noop */ }
|
|
133
|
+
throw e;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A savepoint inside the transaction we are demonstrably running within. */
|
|
138
|
+
async function nested(parent, fn) {
|
|
139
|
+
const ctx = context(parent.depth + 1);
|
|
140
|
+
const sp = `aria_sp_${ctx.depth}`;
|
|
141
|
+
db.exec(`SAVEPOINT ${sp}`);
|
|
142
|
+
try {
|
|
143
|
+
const out = await txContext.run(ctx, () => fn(api));
|
|
144
|
+
db.exec(`RELEASE ${sp}`);
|
|
145
|
+
return out;
|
|
146
|
+
} catch (e) {
|
|
147
|
+
// ROLLBACK TO rewinds to the savepoint but LEAVES it on the stack. Releasing it as well
|
|
148
|
+
// is what keeps the stack balanced for whatever the outer transaction does next.
|
|
149
|
+
try { db.exec(`ROLLBACK TO ${sp}`); db.exec(`RELEASE ${sp}`); } catch (_) { /* noop */ }
|
|
150
|
+
throw e;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
81
153
|
|
|
82
154
|
const api = {
|
|
83
155
|
kind: 'sqlite',
|
|
@@ -107,29 +179,28 @@ function wrap(db) {
|
|
|
107
179
|
/**
|
|
108
180
|
* Transactions are run with explicit BEGIN/COMMIT rather than better-sqlite3's
|
|
109
181
|
* `db.transaction(fn)`, because that helper REFUSES an async callback — and every callback
|
|
110
|
-
* here is async by contract.
|
|
111
|
-
*
|
|
182
|
+
* here is async by contract.
|
|
183
|
+
*
|
|
184
|
+
* Two paths, and telling them apart is the entire point:
|
|
185
|
+
*
|
|
186
|
+
* inside another transaction on THIS connection -> SAVEPOINT, so the inner half can fail
|
|
187
|
+
* without discarding the outer work
|
|
188
|
+
* anywhere else -> a real BEGIN, QUEUED behind whatever
|
|
189
|
+
* is already open on this connection
|
|
190
|
+
*
|
|
191
|
+
* The queue is what makes overlap safe rather than merely detected. SQLite allows one write
|
|
192
|
+
* transaction per connection, so waiting costs nothing that was ever available anyway.
|
|
193
|
+
*
|
|
194
|
+
* A nested call whose async context was somehow lost would be mistaken for a top-level one
|
|
195
|
+
* and queue behind the transaction it is running inside — a hang. AsyncLocalStorage
|
|
196
|
+
* propagates through promises, timers and microtasks, which is all any callback here uses,
|
|
197
|
+
* and test/transactions.js holds that down across await, setImmediate and Promise.all.
|
|
112
198
|
*/
|
|
113
199
|
async transaction(fn) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
// savepoints that can roll back independently without discarding the outer work.
|
|
119
|
-
const depth = db[DEPTH]++;
|
|
120
|
-
const sp = `aria_sp_${depth}`;
|
|
121
|
-
db.exec(depth === 0 ? 'BEGIN' : `SAVEPOINT ${sp}`);
|
|
122
|
-
try {
|
|
123
|
-
const out = await fn(api);
|
|
124
|
-
db.exec(depth === 0 ? 'COMMIT' : `RELEASE ${sp}`);
|
|
125
|
-
return out;
|
|
126
|
-
} catch (e) {
|
|
127
|
-
// The original error is what matters; a failed rollback must not mask it.
|
|
128
|
-
try { db.exec(depth === 0 ? 'ROLLBACK' : `ROLLBACK TO ${sp}`); } catch (_) { /* noop */ }
|
|
129
|
-
throw e;
|
|
130
|
-
} finally {
|
|
131
|
-
db[DEPTH] = depth;
|
|
132
|
-
}
|
|
200
|
+
const parent = txContext.getStore();
|
|
201
|
+
if (parent && parent.db === db) return enqueue(parent, () => nested(parent, fn));
|
|
202
|
+
// No parent on this connection: a top-level transaction, queued on the connection itself.
|
|
203
|
+
return enqueue(db[QUEUE_OWNER], () => top(fn));
|
|
133
204
|
},
|
|
134
205
|
|
|
135
206
|
async close() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
|
@@ -37,6 +37,6 @@
|
|
|
37
37
|
"pg": "^8.23.0"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
|
-
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/regressions.js && node test/smoke.js && node test/tier2.js"
|
|
40
|
+
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/regressions.js && node test/transactions.js && node test/smoke.js && node test/tier2.js"
|
|
41
41
|
}
|
|
42
42
|
}
|