durable-db 0.15.0 → 0.15.1
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 +3 -1
- package/dist/db/index.js +15 -8
- package/dist/{kysely-BUmrdN9f.js → kysely-DtVW6lal.js} +22 -1
- package/dist/testing/index.js +15 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -303,6 +303,8 @@ export const postVoucher = action({
|
|
|
303
303
|
await db.insertInto('waitlist').values(args).execute();
|
|
304
304
|
}
|
|
305
305
|
```
|
|
306
|
+
- **Await every query.** A query, a push to the outbox or a part that runs after its action (or its part) has ended is refused: one the handler did not await, or the other promise of a `Promise.all()` that gave up when one failed. Run after the end, it would not be in the transaction — written on its own after the action rolled back, or into the action after its part did — so it throws `… ran after its action ended …` instead (an unhandled rejection, since nothing awaits it). Run the writes one after the other, or with `Promise.allSettled()`, before the handler returns.
|
|
307
|
+
|
|
306
308
|
- **Kysely's `db.transaction()`** is refused, naming `ctx.transaction()`: the action already is one, and a Durable Object refuses SQL's `BEGIN` and `SAVEPOINT` anyway.
|
|
307
309
|
- **An action holds its object.** An action that waits on I/O — another database's action, a `fetch` — holds its object for that long, every other call of it waiting. Keep slow work out of actions (call it from the worker, or [push it to the outbox](#the-outbox)). The runtime resets an object whose action runs for more than 30 s, and the action's writes are rolled back.
|
|
308
310
|
- **Across objects nothing is atomic.** An action of another database (another object, or another tenant's) runs as a transaction of its own and commits on its own: when the calling action then throws, what the other one wrote stays. Use the outbox for the step that must follow a commit.
|
|
@@ -1052,7 +1054,7 @@ t.destroy();
|
|
|
1052
1054
|
|
|
1053
1055
|
While a test database is live, an action called directly — from a page, a procedure, a job under test — runs against the newest live test database created with every table of the action's database.
|
|
1054
1056
|
|
|
1055
|
-
An action runs there as [in its Durable Object](#one-transaction): one transaction (`BEGIN … COMMIT`, rolled back when it throws), one at a time per test database — a run started while another runs waits for it —, `ctx.transaction()` and an action of the same database called from one as a savepoint, Kysely's `db.transaction()` refused, and a call back into a test database an action further out runs in refused with the `ActionCycleError` production gives. `t.runHandler(handler, args)` runs a handler whose args are already validated the same way, for a library that finds an action's handler itself.
|
|
1057
|
+
An action runs there as [in its Durable Object](#one-transaction): one transaction (`BEGIN … COMMIT`, rolled back when it throws), one at a time per test database — a run started while another runs waits for it —, `ctx.transaction()` and an action of the same database called from one as a savepoint, Kysely's `db.transaction()` refused, a query or a push after its action ended refused, and a call back into a test database an action further out runs in refused with the `ActionCycleError` production gives. `t.runHandler(handler, args)` runs a handler whose args are already validated the same way, for a library that finds an action's handler itself.
|
|
1056
1058
|
|
|
1057
1059
|
---
|
|
1058
1060
|
|
package/dist/db/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { a as setDebugSpec, i as init_debug, n as debugMigrations } from "../deb
|
|
|
2
2
|
import { a as createRelated, c as DrizzleDefaultsPlugin, d as createJsonPlugins, f as dateSerializers, h as relationsOf, i as RelatedExpression, l as SchemaPlugin, m as relationOf, n as defineDatabase, p as parseJsonValue, s as DateSerializePlugin, u as createDrizzlePlugins } from "../defineDatabase-xd-9vnhm.js";
|
|
3
3
|
import { r as type } from "../type-C7apBBQw.js";
|
|
4
4
|
import "../actionArgs-BAmU_AIB.js";
|
|
5
|
-
import { a as OUTBOX_DEAD_TABLE, c as OUTBOX_TABLE, d as outboxRetryDelay, f as outboxSink, h as tenantOfInstanceKey, i as OUTBOX_DEAD_DDL, l as createActionOutbox, m as settleOutboxDelivery, n as OUTBOX_ADDED_COLUMNS, o as OUTBOX_SINK_DDL, p as queueOutboxSink, r as OUTBOX_DDL, s as OUTBOX_SINK_TABLE, t as createKyselyFromSql, u as outboxErrorText } from "../kysely-
|
|
5
|
+
import { _ as inScope, a as OUTBOX_DEAD_TABLE, c as OUTBOX_TABLE, d as outboxRetryDelay, f as outboxSink, g as assertInScope, h as tenantOfInstanceKey, i as OUTBOX_DEAD_DDL, l as createActionOutbox, m as settleOutboxDelivery, n as OUTBOX_ADDED_COLUMNS, o as OUTBOX_SINK_DDL, p as queueOutboxSink, r as OUTBOX_DDL, s as OUTBOX_SINK_TABLE, t as createKyselyFromSql, u as outboxErrorText } from "../kysely-DtVW6lal.js";
|
|
6
6
|
import { t as guardReferencingRows } from "../referencingRows-B6rU7TDW.js";
|
|
7
7
|
import { CamelCasePlugin } from "kysely";
|
|
8
8
|
import { DurableObject } from "cloudflare:workers";
|
|
@@ -281,19 +281,26 @@ var SqliteDurableObject = class extends DurableObject {
|
|
|
281
281
|
if (ctx.outbox) return ctx.transaction ? ctx.transaction(run) : storage.transaction(() => run());
|
|
282
282
|
const outbox = createActionOutbox(this.outboxSink !== null);
|
|
283
283
|
ctx.outbox = outbox;
|
|
284
|
-
ctx.transaction = (fn) =>
|
|
285
|
-
const mark = outbox.mark();
|
|
284
|
+
ctx.transaction = (fn) => {
|
|
286
285
|
try {
|
|
287
|
-
|
|
286
|
+
assertInScope("ctx.transaction()");
|
|
288
287
|
} catch (error) {
|
|
289
|
-
|
|
290
|
-
throw error;
|
|
288
|
+
return Promise.reject(error);
|
|
291
289
|
}
|
|
292
|
-
|
|
290
|
+
return storage.transaction(async () => {
|
|
291
|
+
const mark = outbox.mark();
|
|
292
|
+
try {
|
|
293
|
+
return await inScope(fn);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
outbox.rewind(mark);
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
};
|
|
293
300
|
const outboxWasReady = this.outboxReady;
|
|
294
301
|
try {
|
|
295
302
|
return await storage.transaction(async () => {
|
|
296
|
-
const result = await run
|
|
303
|
+
const result = await inScope(run);
|
|
297
304
|
const messages = outbox.take();
|
|
298
305
|
if (messages.length > 0) {
|
|
299
306
|
this.ensureOutboxTable();
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import { l as SchemaPlugin, o as createRelatedPlugins, s as DateSerializePlugin, u as createDrizzlePlugins } from "./defineDatabase-xd-9vnhm.js";
|
|
2
2
|
import { CamelCasePlugin, Kysely, SqliteDialect, SqliteQueryCompiler } from "kysely";
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
//#region src/db/scope.ts
|
|
5
|
+
const SCOPES = Symbol.for("durable-db.actionScopes");
|
|
6
|
+
const scopes = globalThis[SCOPES] ??= new AsyncLocalStorage();
|
|
7
|
+
/** Runs `fn` as code of a transaction that ends when `fn` settles. */
|
|
8
|
+
async function inScope(fn) {
|
|
9
|
+
const scope = { open: true };
|
|
10
|
+
try {
|
|
11
|
+
return await scopes.run(scope, fn);
|
|
12
|
+
} finally {
|
|
13
|
+
scope.open = false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Throws when the code running now belongs to a transaction that has ended. */
|
|
17
|
+
function assertInScope(what) {
|
|
18
|
+
const scope = scopes.getStore();
|
|
19
|
+
if (scope && !scope.open) throw new Error(`[db] ${what} ran after its action ended, so it would not be part of the action's transaction — a promise the action did not await, or one Promise.all() left behind when another failed. Await each query and push (one after the other, or with Promise.allSettled()) before the action returns.`);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
3
22
|
//#region src/db/outbox.ts
|
|
4
23
|
/** A sink module's default export as a sink: a deliver function, or an object with one. */
|
|
5
24
|
function outboxSink(module) {
|
|
@@ -257,6 +276,7 @@ function createActionOutbox(sinkConfigured) {
|
|
|
257
276
|
let messages = [];
|
|
258
277
|
return {
|
|
259
278
|
push(message) {
|
|
279
|
+
assertInScope("ctx.outbox.push()");
|
|
260
280
|
if (!sinkConfigured) throw new Error("[db] ctx.outbox.push(): no outbox sink is configured — durableDb({ outbox: { queue: \"<QUEUE_BINDING>\" } }) or durableDb({ outbox: { module: \"./src/outbox.ts\" } })");
|
|
261
281
|
const json = JSON.stringify(message);
|
|
262
282
|
if (json === void 0) throw new TypeError("[db] ctx.outbox.push(): a message must be a JSON value");
|
|
@@ -409,6 +429,7 @@ var CloudflareSqliteConnection = class {
|
|
|
409
429
|
this.sql = sql;
|
|
410
430
|
}
|
|
411
431
|
async executeQuery(compiledQuery) {
|
|
432
|
+
assertInScope("A query");
|
|
412
433
|
let queryStr = compiledQuery.sql;
|
|
413
434
|
let parameters = compiledQuery.parameters.map(normalizeParameter);
|
|
414
435
|
if (parameters.length > MAX_BOUND_PARAMETERS) {
|
|
@@ -472,4 +493,4 @@ function createKyselyFromSql(sqlStorage, options = {}) {
|
|
|
472
493
|
});
|
|
473
494
|
}
|
|
474
495
|
//#endregion
|
|
475
|
-
export { OUTBOX_DEAD_TABLE as a, OUTBOX_TABLE as c, outboxRetryDelay as d, outboxSink as f, tenantOfInstanceKey as h, OUTBOX_DEAD_DDL as i, createActionOutbox as l, settleOutboxDelivery as m, OUTBOX_ADDED_COLUMNS as n, OUTBOX_SINK_DDL as o, queueOutboxSink as p, OUTBOX_DDL as r, OUTBOX_SINK_TABLE as s, createKyselyFromSql as t, outboxErrorText as u };
|
|
496
|
+
export { inScope as _, OUTBOX_DEAD_TABLE as a, OUTBOX_TABLE as c, outboxRetryDelay as d, outboxSink as f, assertInScope as g, tenantOfInstanceKey as h, OUTBOX_DEAD_DDL as i, createActionOutbox as l, settleOutboxDelivery as m, OUTBOX_ADDED_COLUMNS as n, OUTBOX_SINK_DDL as o, queueOutboxSink as p, OUTBOX_DDL as r, OUTBOX_SINK_TABLE as s, createKyselyFromSql as t, outboxErrorText as u };
|
package/dist/testing/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { r as liveTestDatabases, t as ACTION_TEST_META } from "../defineDatabase
|
|
|
2
2
|
import { r as type } from "../type-C7apBBQw.js";
|
|
3
3
|
import "../actionArgs-BAmU_AIB.js";
|
|
4
4
|
import { setTenantIdResolver } from "../context/index.js";
|
|
5
|
-
import { h as tenantOfInstanceKey, l as createActionOutbox, t as createKyselyFromSql } from "../kysely-
|
|
5
|
+
import { _ as inScope, g as assertInScope, h as tenantOfInstanceKey, l as createActionOutbox, t as createKyselyFromSql } from "../kysely-DtVW6lal.js";
|
|
6
6
|
import { i as generateSnapshotFromSchema, n as createEmptySnapshot, r as generateMigrationStatements } from "../snapshot-4RsNw1yr.js";
|
|
7
7
|
import { ActionCycleError } from "../registry.js";
|
|
8
8
|
import { createRequire } from "node:module";
|
|
@@ -74,7 +74,7 @@ async function inTransaction(sqlite, transactions, ctx, fn) {
|
|
|
74
74
|
transactions,
|
|
75
75
|
level,
|
|
76
76
|
ctx
|
|
77
|
-
}], fn);
|
|
77
|
+
}], () => inScope(fn));
|
|
78
78
|
sqlite.exec(depth === 1 ? "COMMIT" : `RELEASE ${savepoint}`);
|
|
79
79
|
return result;
|
|
80
80
|
} catch (error) {
|
|
@@ -88,15 +88,22 @@ async function inTransaction(sqlite, transactions, ctx, fn) {
|
|
|
88
88
|
}
|
|
89
89
|
/** A part of the action of `ctx`: a savepoint, and what it pushed goes with it when it throws. */
|
|
90
90
|
function partOf(sqlite, transactions, ctx) {
|
|
91
|
-
return (fn) =>
|
|
92
|
-
const mark = ctx.outbox.mark();
|
|
91
|
+
return (fn) => {
|
|
93
92
|
try {
|
|
94
|
-
|
|
93
|
+
assertInScope("ctx.transaction()");
|
|
95
94
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
throw error;
|
|
95
|
+
return Promise.reject(error);
|
|
98
96
|
}
|
|
99
|
-
|
|
97
|
+
return inTransaction(sqlite, transactions, ctx, async () => {
|
|
98
|
+
const mark = ctx.outbox.mark();
|
|
99
|
+
try {
|
|
100
|
+
return await fn();
|
|
101
|
+
} catch (error) {
|
|
102
|
+
ctx.outbox.rewind(mark);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
};
|
|
100
107
|
}
|
|
101
108
|
async function deriveStatements(schema) {
|
|
102
109
|
const snapshot = await generateSnapshotFromSchema(schema);
|