durable-db 0.2.2 → 0.4.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 +175 -25
- package/dist/{arktypeConfig-DxP0OcV_.js → actionArgs-1hEaDUia.js} +35 -1
- package/dist/cli/bin.js +1 -1
- package/dist/cli/index.d.ts +204 -62
- package/dist/cli/index.js +4 -3
- package/dist/{cli-BUSZTS2D.js → cli-CVS0b2im.js} +272 -95
- package/dist/context/index.js +14 -8
- package/dist/db/index.d.ts +2 -3
- package/dist/db/index.js +216 -25
- package/dist/defineDatabase-D_RpcKj1.js +109 -0
- package/dist/{defineDatabase-IUthY3Ex.d.ts → defineDatabase-zrUJrMd5.d.ts} +10 -3
- package/dist/{index-BdrcuAU7.d.ts → index-CJ6Focq4.d.ts} +70 -17
- package/dist/index.d.ts +1 -2
- package/dist/index.js +2 -2
- package/dist/{kysely-Be6KwFZC.js → kysely-CYmmiwDm.js} +25 -5
- package/dist/migrations/generator.d.ts +1 -1
- package/dist/migrations/generator.js +1 -1
- package/dist/migrations/snapshot.d.ts +2 -2
- package/dist/migrations/snapshot.js +1 -91
- package/dist/{parser-DR57GOjv.js → parser-DyepmXzE.js} +1 -1
- package/dist/registry.d.ts +29 -2
- package/dist/registry.js +2 -2
- package/dist/snapshot-B9Ae2TrS.d.ts +180 -0
- package/dist/snapshot-D_z95s6E.js +459 -0
- package/dist/{state-OVuoHRC9.js → state-DGYycIoJ.js} +106 -9
- package/dist/testing/index.d.ts +8 -1
- package/dist/testing/index.js +30 -11
- package/dist/vite/durableDb.d.ts +130 -1
- package/dist/vite/durableDb.js +733 -180
- package/package.json +15 -14
- package/virtual.d.ts +4 -2
- package/dist/arktypeConfig-AXvobTPL.d.ts +0 -5
- package/dist/defineDatabase-CHmzpzFf.js +0 -70
- package/dist/snapshot-CaG5xnE2.d.ts +0 -48
package/dist/testing/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as ACTION_TEST_META } from "../defineDatabase-
|
|
1
|
+
import { n as type } from "../actionArgs-1hEaDUia.js";
|
|
2
|
+
import { r as liveTestDatabases, t as ACTION_TEST_META } from "../defineDatabase-D_RpcKj1.js";
|
|
3
3
|
import { setTenantIdResolver } from "../context/index.js";
|
|
4
|
-
import { t as createKyselyFromSql } from "../kysely-
|
|
5
|
-
import { createEmptySnapshot, generateMigrationStatements
|
|
4
|
+
import { t as createKyselyFromSql } from "../kysely-CYmmiwDm.js";
|
|
5
|
+
import { i as generateSnapshotFromSchema, n as createEmptySnapshot, r as generateMigrationStatements } from "../snapshot-D_z95s6E.js";
|
|
6
6
|
import { createRequire } from "node:module";
|
|
7
7
|
//#region src/testing/index.ts
|
|
8
8
|
const loadSqlite = () => createRequire(import.meta.url)("node:sqlite");
|
|
@@ -48,17 +48,36 @@ async function createTestDatabase(options) {
|
|
|
48
48
|
const ddl = statements ?? await deriveStatements(schema);
|
|
49
49
|
for (const stmt of ddl) sqlite.exec(stmt);
|
|
50
50
|
setTenantIdResolver(() => tenantId);
|
|
51
|
+
async function run(action, args) {
|
|
52
|
+
const meta = action[ACTION_TEST_META];
|
|
53
|
+
if (!meta) throw new Error("createTestDatabase().run() was given a function that is not a durable-db action (no test metadata). Pass an export created by `action({ … })`.");
|
|
54
|
+
const validated = meta.validator(args);
|
|
55
|
+
if (validated instanceof type.errors) throw new Error(`Invalid args for action: ${validated.summary}`);
|
|
56
|
+
return await meta.handler(db, validated);
|
|
57
|
+
}
|
|
58
|
+
const live = {
|
|
59
|
+
tables: new Set(Object.values(schema)),
|
|
60
|
+
run: (action, args) => run(action, args),
|
|
61
|
+
clear() {
|
|
62
|
+
const tables = sqlite.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite\\_%' ESCAPE '\\'`).all();
|
|
63
|
+
const { foreign_keys: foreignKeys } = sqlite.prepare("PRAGMA foreign_keys").get();
|
|
64
|
+
sqlite.exec("PRAGMA foreign_keys = OFF");
|
|
65
|
+
try {
|
|
66
|
+
for (const { name } of tables) sqlite.exec(`DELETE FROM \`${name.replaceAll("`", "``")}\``);
|
|
67
|
+
} finally {
|
|
68
|
+
sqlite.exec(`PRAGMA foreign_keys = ${foreignKeys ? "ON" : "OFF"}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
liveTestDatabases().push(live);
|
|
51
73
|
return {
|
|
52
74
|
db,
|
|
53
|
-
|
|
54
|
-
const meta = action[ACTION_TEST_META];
|
|
55
|
-
if (!meta) throw new Error("createTestDatabase().run() was given a function that is not a durable-db action (no test metadata). Pass an export created by `action({ … })`.");
|
|
56
|
-
const validated = meta.validator(args);
|
|
57
|
-
if (validated instanceof type.errors) throw new Error(`Invalid args for action: ${validated.summary}`);
|
|
58
|
-
return await meta.handler(db, validated);
|
|
59
|
-
},
|
|
75
|
+
run,
|
|
60
76
|
sqlite,
|
|
61
77
|
destroy() {
|
|
78
|
+
const registry = liveTestDatabases();
|
|
79
|
+
const index = registry.indexOf(live);
|
|
80
|
+
if (index !== -1) registry.splice(index, 1);
|
|
62
81
|
setTenantIdResolver(null);
|
|
63
82
|
sqlite.close();
|
|
64
83
|
}
|
package/dist/vite/durableDb.d.ts
CHANGED
|
@@ -1,5 +1,129 @@
|
|
|
1
1
|
import { t as DatabaseSource } from "../discovery-gHeLq8jk.js";
|
|
2
|
+
import "../index-CJ6Focq4.js";
|
|
2
3
|
import { Plugin } from "vite";
|
|
4
|
+
//#region src/vite/modules/wrangler.d.ts
|
|
5
|
+
/** A Durable Object binding as wrangler.jsonc declares it. */
|
|
6
|
+
interface DurableObjectBinding {
|
|
7
|
+
name: string;
|
|
8
|
+
class_name: string;
|
|
9
|
+
}
|
|
10
|
+
/** A class rename written as a `renamed_classes` migration. */
|
|
11
|
+
interface ClassRename {
|
|
12
|
+
from: string;
|
|
13
|
+
to: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A database's migrations directory moved with its class rename: the renamed
|
|
17
|
+
* Durable Object keeps the old database's tables and `__migrations` journal,
|
|
18
|
+
* so the build must embed the migrations that journal names.
|
|
19
|
+
*/
|
|
20
|
+
interface MigrationsMove {
|
|
21
|
+
/** The old database's migrations directory (absolute). */
|
|
22
|
+
from: string;
|
|
23
|
+
/** The renamed database's migrations directory (absolute). */
|
|
24
|
+
to: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A stale class and a new database that look like the same database under a
|
|
28
|
+
* new mount, whose rename is not written: moving the data would leave it
|
|
29
|
+
* without the migrations its journal names, or the old migrations directory
|
|
30
|
+
* is not unique. Neither the rename nor a new class is written for the pair
|
|
31
|
+
* until the user resolves it (the warning says how).
|
|
32
|
+
*/
|
|
33
|
+
interface RenameConflict extends ClassRename {
|
|
34
|
+
reason: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result of wrangler config patching
|
|
38
|
+
*/
|
|
39
|
+
interface PatchResult {
|
|
40
|
+
/** Whether the config was modified */
|
|
41
|
+
modified: boolean;
|
|
42
|
+
/** Path to the config file (if found) */
|
|
43
|
+
configPath: string | null;
|
|
44
|
+
/** Any errors that occurred */
|
|
45
|
+
error: Error | null;
|
|
46
|
+
/** Bindings added for discovered databases. */
|
|
47
|
+
addedBindings: DurableObjectBinding[];
|
|
48
|
+
/**
|
|
49
|
+
* durable-db's own bindings (`<X>_DATABASE_DO` → `<X>DatabaseDO`) removed
|
|
50
|
+
* because their class is no longer a discovered database — the worker no
|
|
51
|
+
* longer exports it, and workerd refuses to start with a binding to it.
|
|
52
|
+
*/
|
|
53
|
+
removedBindings: DurableObjectBinding[];
|
|
54
|
+
/** Classes added to a `new_sqlite_classes` migration. */
|
|
55
|
+
newClasses: string[];
|
|
56
|
+
/** The tag of the migration written for `newClasses` and `renamedClasses`, if any. */
|
|
57
|
+
migrationTag: string | null;
|
|
58
|
+
/**
|
|
59
|
+
* Classes renamed by a `renamed_classes` migration: a database of a mounted
|
|
60
|
+
* source whose class name changed (its mount changed, or it moved into a
|
|
61
|
+
* mounted source) keeps its deployed data.
|
|
62
|
+
*/
|
|
63
|
+
renamedClasses: ClassRename[];
|
|
64
|
+
/** Migrations directories moved with a rename (see {@link MigrationsMove}). */
|
|
65
|
+
movedMigrations: MigrationsMove[];
|
|
66
|
+
/**
|
|
67
|
+
* Renames refused: written neither as a rename nor as a new class until the
|
|
68
|
+
* user resolves them (see {@link RenameConflict}). A production build should
|
|
69
|
+
* fail on them.
|
|
70
|
+
*/
|
|
71
|
+
conflicts: RenameConflict[];
|
|
72
|
+
/**
|
|
73
|
+
* Classes of databases that are gone, still declared by the migrations.
|
|
74
|
+
* Deleting a class deletes its data, so the `deleted_classes` migration is
|
|
75
|
+
* never written automatically — a warning gives the exact entry to add.
|
|
76
|
+
*/
|
|
77
|
+
deletedClasses: string[];
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/vite/modules/syncWrangler.d.ts
|
|
81
|
+
interface SyncWranglerConfigOptions {
|
|
82
|
+
/** Project root (where wrangler.jsonc/json lives). */
|
|
83
|
+
projectRoot: string;
|
|
84
|
+
/** Where database definitions live (default `src/databases`), as for the plugin. */
|
|
85
|
+
databasesDir?: string | DatabaseSource[];
|
|
86
|
+
/**
|
|
87
|
+
* Where production migrations live (default `migrations`), as for the
|
|
88
|
+
* plugin: a mount change moves the database's directory with its class.
|
|
89
|
+
*/
|
|
90
|
+
migrationsDir?: string;
|
|
91
|
+
/**
|
|
92
|
+
* Where the one-line summary of a change goes. Default: `console.log`;
|
|
93
|
+
* `false` silences it.
|
|
94
|
+
*/
|
|
95
|
+
log?: ((message: string) => void) | false;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* What {@link patchWranglerConfig} did (bindings added and removed, classes
|
|
99
|
+
* created and renamed, migrations moved, renames refused, classes left for a
|
|
100
|
+
* hand-written `deleted_classes`), plus the databases and the summary.
|
|
101
|
+
*/
|
|
102
|
+
interface SyncWranglerConfigResult extends PatchResult {
|
|
103
|
+
/** The databases discovered. */
|
|
104
|
+
databases: string[];
|
|
105
|
+
/** Database files that did not parse: while there are any, nothing is written. */
|
|
106
|
+
unparsable: string[];
|
|
107
|
+
/** What changed, for the summary line: Durable Object bindings and the migration written. */
|
|
108
|
+
changes: string[];
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Discover the databases and reconcile wrangler.jsonc/json with them — before
|
|
112
|
+
* anything reads the config. The Vite plugin runs this in its `config` hook
|
|
113
|
+
* (with `patchWranglerConfig: true`), ahead of @cloudflare/vite-plugin, which
|
|
114
|
+
* reads the worker config in its own `config` hook and emits the build's
|
|
115
|
+
* `wrangler.json` from that copy: patched any later, a build ships the
|
|
116
|
+
* previous bindings, and a dev server restarts while it starts. CLIs that want
|
|
117
|
+
* the file settled before Vite starts can call it themselves; a refused rename
|
|
118
|
+
* is in `conflicts` (the plugin stops on it).
|
|
119
|
+
*
|
|
120
|
+
* When a database file does not parse, nothing is written: the patch is
|
|
121
|
+
* computed from the complete set of databases or not at all (a partial set
|
|
122
|
+
* would read as databases removed). The plugin reports the parse error when
|
|
123
|
+
* it loads the file. A missing config file is reported by the patcher.
|
|
124
|
+
*/
|
|
125
|
+
export declare function syncWranglerConfig(options: SyncWranglerConfigOptions): SyncWranglerConfigResult;
|
|
126
|
+
//#endregion
|
|
3
127
|
//#region src/vite/durableDb.d.ts
|
|
4
128
|
export interface DurableDbOptions {
|
|
5
129
|
/** Import path for the context module. Default: 'durable-db/context' */
|
|
@@ -15,6 +139,11 @@ export interface DurableDbOptions {
|
|
|
15
139
|
* wrangler.jsonc/json automatically. When false (the default), the plugin
|
|
16
140
|
* only verifies the config and logs the exact JSON to add when something
|
|
17
141
|
* is missing — your config file is never modified.
|
|
142
|
+
*
|
|
143
|
+
* The write happens in the plugin's `config` hook, before
|
|
144
|
+
* @cloudflare/vite-plugin reads the file, so a build emits a wrangler.json
|
|
145
|
+
* with the new bindings and a dev server does not restart on its first
|
|
146
|
+
* start. A change is logged (`durable-db updated wrangler.jsonc (…)`).
|
|
18
147
|
*/
|
|
19
148
|
patchWranglerConfig?: boolean;
|
|
20
149
|
}
|
|
@@ -24,4 +153,4 @@ export declare const databasePlugin: typeof durableDb;
|
|
|
24
153
|
/** @deprecated Renamed to {@link DurableDbOptions}. */
|
|
25
154
|
export type DatabasePluginOptions = DurableDbOptions;
|
|
26
155
|
//#endregion
|
|
27
|
-
export { type DatabaseSource, durableDb as default };
|
|
156
|
+
export { type DatabaseSource, type SyncWranglerConfigOptions, type SyncWranglerConfigResult, durableDb as default };
|