@aria-framework/db-worker 0.27.2 → 0.28.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/README.md +20 -0
- package/driver/postgres.js +13 -0
- package/driver/sqlite.js +10 -0
- package/package.json +2 -2
- package/worker.js +9 -2
package/README.md
CHANGED
|
@@ -236,6 +236,23 @@ holds it down.
|
|
|
236
236
|
On a serial driver (SQLite) there is no row-level security and nothing is set. The context is
|
|
237
237
|
still established so a model can read it via `currentTenant()`, and no transaction is forced.
|
|
238
238
|
|
|
239
|
+
### `driver.inTransaction()` and `outsideTransaction` (0.28.0)
|
|
240
|
+
|
|
241
|
+
- **`driver.inTransaction()`** answers whether a statement issued NOW would be part of a transaction: on Postgres,
|
|
242
|
+
whether the async call chain is inside a transaction on that pool (the worker's per-invoke one included); on
|
|
243
|
+
SQLite, the connection's own `inTransaction` (one connection, so any open BEGIN counts). A transaction's handle
|
|
244
|
+
answers `true`.
|
|
245
|
+
- **A model method marked `fn.outsideTransaction = true`** runs with the tenant context set but **no** per-invoke
|
|
246
|
+
transaction around it: each statement autocommits on its own pooled connection and carries no transaction-local
|
|
247
|
+
scope. Use it only for tables with no row-level policy (PLATFORM). On a scoped table the strict policy refuses
|
|
248
|
+
the statement, which fails closed.
|
|
249
|
+
- Why: a write that must commit even when the caller then fails. `@aria-framework/auth` 0.18.0's attempt claim is
|
|
250
|
+
one — inside a transaction, the rollback a failed sign-in causes erased it, and the guesses were unlimited.
|
|
251
|
+
`test/inTransaction.js` proves both on every run: on SQLite, and on the POOLED path through PGlite behind a
|
|
252
|
+
pooled-mode test driver (`test/pglitePoolDriver.js`, a non-superuser session) — SQLite's worker never reads the
|
|
253
|
+
mark, so only a pooled run can catch a worker that ignores it. It also writes a FORCE-RLS table from a marked
|
|
254
|
+
method and expects the policy to refuse it. With `ARIA_TEST_PG` the same runs on real Postgres.
|
|
255
|
+
|
|
239
256
|
## Migrations per engine (0.12.0)
|
|
240
257
|
|
|
241
258
|
A migration file may name the engine it is for. The runner picks; it never translates.
|
|
@@ -437,6 +454,9 @@ Against 0.25.0's code, 6 of its 10 checks fail.
|
|
|
437
454
|
|
|
438
455
|
## Changelog
|
|
439
456
|
|
|
457
|
+
- **0.28.0** — `driver.inTransaction()` on both drivers, and a model method's `outsideTransaction = true` skips the
|
|
458
|
+
worker's per-invoke transaction on a pooled driver (above). Additive: no method is marked unless the app marks it.
|
|
459
|
+
For `@aria-framework/auth` 0.18.0's SQL attempt store, which refuses a claim inside a transaction.
|
|
440
460
|
- **0.27.2** — a FAILED MIGRATION REJECTS `runMigrations` again on the Postgres migration-connection path.
|
|
441
461
|
Its `finally` did `return` after logging "Skipping the BYPASSRLS proof", and a `return` in a finally discards the
|
|
442
462
|
exception in flight: `runMigrations` resolved, the app booted on a half-migrated schema, and the next migrations
|
package/driver/postgres.js
CHANGED
|
@@ -191,6 +191,8 @@ function createPostgresDriver(opts = {}) {
|
|
|
191
191
|
async exec(sql) {
|
|
192
192
|
await exec(sql); // DDL: never parameterised, so no rewrite
|
|
193
193
|
},
|
|
194
|
+
// 0.28.0 — inside a transaction's own handle, always true. Overridden for the pool below.
|
|
195
|
+
inTransaction: () => true,
|
|
194
196
|
...extra
|
|
195
197
|
});
|
|
196
198
|
|
|
@@ -215,6 +217,17 @@ function createPostgresDriver(opts = {}) {
|
|
|
215
217
|
};
|
|
216
218
|
|
|
217
219
|
const poolApi = apiOver(poolExec, {
|
|
220
|
+
/**
|
|
221
|
+
* 0.28.0 — would a statement issued NOW through this driver be part of a transaction? The
|
|
222
|
+
* same question poolExec answers when it routes: yes exactly when the async call chain is
|
|
223
|
+
* inside a transaction on THIS pool (including the worker's per-invoke one). Otherwise the
|
|
224
|
+
* statement autocommits on its own pooled connection. A store whose write must outlive the
|
|
225
|
+
* caller's failure (an attempt claim) refuses on true.
|
|
226
|
+
*/
|
|
227
|
+
inTransaction() {
|
|
228
|
+
const parent = txContext.getStore();
|
|
229
|
+
return !!(parent && parent.pool === pool);
|
|
230
|
+
},
|
|
218
231
|
/**
|
|
219
232
|
* A dedicated client for the whole transaction. Without this, a pooled query from another
|
|
220
233
|
* concurrent invoke could land between the BEGIN and the COMMIT — the same interleaving
|
package/driver/sqlite.js
CHANGED
|
@@ -203,6 +203,16 @@ function wrap(db) {
|
|
|
203
203
|
return enqueue(db[QUEUE_OWNER], () => top(fn));
|
|
204
204
|
},
|
|
205
205
|
|
|
206
|
+
/**
|
|
207
|
+
* 0.28.0 — would a statement issued NOW be part of a transaction? One connection, so the
|
|
208
|
+
* connection's own answer is the truth whoever opened the transaction: a statement run while
|
|
209
|
+
* any BEGIN is open commits or rolls back with it. A store whose write must outlive the
|
|
210
|
+
* caller's failure (an attempt claim) refuses on true.
|
|
211
|
+
*/
|
|
212
|
+
inTransaction() {
|
|
213
|
+
return db.inTransaction === true;
|
|
214
|
+
},
|
|
215
|
+
|
|
206
216
|
async close() {
|
|
207
217
|
db.close();
|
|
208
218
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.28.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,
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"pg": "^8.23.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
|
-
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationFailureSurfaces.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js",
|
|
43
|
+
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/migrationFailureSurfaces.js && node test/migrationDialect.js && node test/regressions.js && node test/transactions.js && node test/tenantContext.js && node test/tenancyMigration.js && node test/tenancyAttribution.pglite.js && node test/tenancyNullable.pg.js && node test/tenancyPartnerFk.pg.js && node test/tenancyOnUpdate.pg.js && node test/dialectIntrospection.js && node test/tenantRls.pg.js && node test/nestedTransaction.pg.js && node test/clientError.pg.js && node test/migrationRole.pg.js && node test/smoke.js && node test/tier2.js && node test/timezone.pg.js && node test/pinPoolUtc.js && node test/pinAwaited.pg.js && node test/inTransaction.js",
|
|
44
44
|
"prepublishOnly": "node ../../test/packaging.js db-worker"
|
|
45
45
|
}
|
|
46
46
|
}
|
package/worker.js
CHANGED
|
@@ -43,7 +43,7 @@ function currentDriver() {
|
|
|
43
43
|
* On a serial driver (SQLite) there is no RLS and nothing to set. The context is still
|
|
44
44
|
* established so a model can read it, and no transaction is forced around a read.
|
|
45
45
|
*/
|
|
46
|
-
async function withTenant(tenant, label, dispatch) {
|
|
46
|
+
async function withTenant(tenant, label, dispatch, { outsideTransaction = false } = {}) {
|
|
47
47
|
const driver = currentDriver();
|
|
48
48
|
if (!driver || driver.concurrency !== 'pool') {
|
|
49
49
|
return tenant ? runWithTenant(tenant, dispatch) : dispatch();
|
|
@@ -56,6 +56,13 @@ async function withTenant(tenant, label, dispatch) {
|
|
|
56
56
|
'Wrap the caller in runWithTenant({ scope, partnerId }, ...).'
|
|
57
57
|
);
|
|
58
58
|
}
|
|
59
|
+
// 0.28.0 — a method that DECLARES `outsideTransaction = true` runs with the context set but no
|
|
60
|
+
// transaction around it: each statement autocommits on its own pooled connection and carries NO
|
|
61
|
+
// transaction-local scope. Only for statements on tables with no row-level policy (a PLATFORM
|
|
62
|
+
// table); on a scoped table the strict policy refuses them, which fails closed. It exists for
|
|
63
|
+
// writes that must commit even when the caller then fails — an attempt claim
|
|
64
|
+
// (@aria-framework/auth 0.18.0), which a rollback would otherwise erase.
|
|
65
|
+
if (outsideTransaction) return runWithTenant(tenant, dispatch);
|
|
59
66
|
return runWithTenant(tenant, () => driver.transaction(async (tx) => {
|
|
60
67
|
await tx.run(APPLY_SQL, applyParams(tenant));
|
|
61
68
|
return dispatch();
|
|
@@ -132,7 +139,7 @@ async function runWorker({ init }) {
|
|
|
132
139
|
r = await r; // async methods (e.g. bcrypt-backed user ops)
|
|
133
140
|
}
|
|
134
141
|
return r;
|
|
135
|
-
});
|
|
142
|
+
}, { outsideTransaction: target[method].outsideTransaction === true });
|
|
136
143
|
|
|
137
144
|
result = sanitizeForTransfer(result);
|
|
138
145
|
parentPort.postMessage({ id, result, execMs: Math.round(performance.now() - t0) });
|