@aria-framework/db-worker 0.12.0 → 0.13.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/postgres.js +48 -16
- package/package.json +1 -1
package/driver/postgres.js
CHANGED
|
@@ -165,24 +165,56 @@ function createPostgresDriver(opts = {}) {
|
|
|
165
165
|
*/
|
|
166
166
|
async transaction(fn) {
|
|
167
167
|
const client = await pool.connect();
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
168
|
+
const q = (sql, params) => client.query(sql, params);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* ONE API PER NESTING LEVEL, each owning the queue for its own children.
|
|
172
|
+
*
|
|
173
|
+
* A shared depth counter is not enough, and the reason is that a savepoint stack is not a
|
|
174
|
+
* set: RELEASE frees every savepoint opened after the named one. Two siblings started
|
|
175
|
+
* together therefore destroy each other as soon as the first finishes — the second's
|
|
176
|
+
* RELEASE finds nothing, that error aborts the transaction, and the outer COMMIT silently
|
|
177
|
+
* becomes a ROLLBACK. The outer call then reports success having written nothing.
|
|
178
|
+
*
|
|
179
|
+
* So siblings take turns. Only one transaction may be innermost on a client at a time.
|
|
180
|
+
* (The SQLite driver has the same rule for the same reason; it reached it first because
|
|
181
|
+
* it also has to serialise TOP-level transactions, which a pool does not.)
|
|
182
|
+
*/
|
|
183
|
+
const levelApi = (depth) => {
|
|
184
|
+
let queue = Promise.resolve();
|
|
185
|
+
return apiOver(q, {
|
|
186
|
+
// Nested transaction() reuses the same CLIENT (a second pooled connection would
|
|
187
|
+
// deadlock against this one's locks) and brackets the body in a SAVEPOINT, so an
|
|
188
|
+
// inner failure rolls back only its own work rather than discarding the outer.
|
|
189
|
+
async transaction(inner) {
|
|
190
|
+
const run = () => runNested(inner, depth + 1);
|
|
191
|
+
// Chained on BOTH settle paths, so one failed child does not wedge its siblings.
|
|
192
|
+
const mine = queue.then(run, run);
|
|
193
|
+
queue = mine.then(() => {}, () => {});
|
|
194
|
+
return mine;
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
async function runNested(inner, depth) {
|
|
200
|
+
const sp = `aria_sp_${depth}`;
|
|
201
|
+
await q(`SAVEPOINT ${sp}`);
|
|
202
|
+
try {
|
|
203
|
+
const out = await inner(levelApi(depth));
|
|
204
|
+
await q(`RELEASE SAVEPOINT ${sp}`);
|
|
205
|
+
return out;
|
|
206
|
+
} catch (e) {
|
|
207
|
+
// ROLLBACK TO rewinds to the savepoint but LEAVES it on the stack. Releasing it as
|
|
208
|
+
// well keeps the stack balanced for whatever the outer transaction does next.
|
|
176
209
|
try {
|
|
177
|
-
|
|
178
|
-
await
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
try { await client.query(`ROLLBACK TO SAVEPOINT ${sp}`); } catch (_) { /* noop */ }
|
|
182
|
-
throw e;
|
|
183
|
-
} finally { depth--; }
|
|
210
|
+
await q(`ROLLBACK TO SAVEPOINT ${sp}`);
|
|
211
|
+
await q(`RELEASE SAVEPOINT ${sp}`);
|
|
212
|
+
} catch (_) { /* noop */ }
|
|
213
|
+
throw e;
|
|
184
214
|
}
|
|
185
|
-
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const txApi = levelApi(0);
|
|
186
218
|
try {
|
|
187
219
|
await client.query('BEGIN');
|
|
188
220
|
const out = await fn(txApi);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.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,
|