durable-db 0.0.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.
Files changed (46) hide show
  1. package/README.md +1052 -0
  2. package/dist/cli/bin.d.ts +1 -0
  3. package/dist/cli/bin.js +18 -0
  4. package/dist/cli/index.d.ts +330 -0
  5. package/dist/cli/index.js +9 -0
  6. package/dist/cli-Cr9686BF.js +630 -0
  7. package/dist/context/index.d.ts +2 -0
  8. package/dist/context/index.js +5 -0
  9. package/dist/context-BuIiIQ6K.js +103 -0
  10. package/dist/db/index.d.ts +3 -0
  11. package/dist/db/index.js +760 -0
  12. package/dist/debug-BZJbKybf.js +49 -0
  13. package/dist/defineDatabase-CONf4xFC.js +54 -0
  14. package/dist/defineDatabase-jBv90-Pk.d.ts +188 -0
  15. package/dist/generator-BUPP9scJ.js +180 -0
  16. package/dist/index-BFM5d0FP.d.ts +328 -0
  17. package/dist/index-CqVMaMYY.d.ts +65 -0
  18. package/dist/index.d.ts +6 -0
  19. package/dist/index.js +8 -0
  20. package/dist/migrations/generator.d.ts +101 -0
  21. package/dist/migrations/generator.js +5 -0
  22. package/dist/migrations/snapshot.d.ts +2 -0
  23. package/dist/migrations/snapshot.js +3 -0
  24. package/dist/parser-CbX54Flt.js +293 -0
  25. package/dist/protocol-g_plhPOY.js +35 -0
  26. package/dist/push-BgQ5cnfH.js +184 -0
  27. package/dist/push-BkkaslPd.js +8 -0
  28. package/dist/registry.d.ts +28 -0
  29. package/dist/registry.js +72 -0
  30. package/dist/schema-BOtrGnzM.js +8 -0
  31. package/dist/schema-CpzTOfZ6.d.ts +6 -0
  32. package/dist/schema.d.ts +2 -0
  33. package/dist/schema.js +3 -0
  34. package/dist/snapshot-C162qYoA.d.ts +125 -0
  35. package/dist/snapshot-D170nL7i.js +93 -0
  36. package/dist/state-BCcvNN-M.js +5 -0
  37. package/dist/state-FOEjpgJ6.js +203 -0
  38. package/dist/transport/protocol.d.ts +27 -0
  39. package/dist/transport/protocol.js +3 -0
  40. package/dist/transport/websocket.d.ts +31 -0
  41. package/dist/transport/websocket.js +3 -0
  42. package/dist/vite/durableDb.d.ts +20 -0
  43. package/dist/vite/durableDb.js +933 -0
  44. package/dist/websocket-Dx1fqwnF.js +104 -0
  45. package/package.json +114 -0
  46. package/virtual.d.ts +15 -0
@@ -0,0 +1 @@
1
+ export { };
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env node
2
+ import "../debug-BZJbKybf.js";
3
+ import "../state-FOEjpgJ6.js";
4
+ import "../snapshot-D170nL7i.js";
5
+ import "../generator-BUPP9scJ.js";
6
+ import "../parser-CbX54Flt.js";
7
+ import "../push-BgQ5cnfH.js";
8
+ import { n as registerDbCommands } from "../cli-Cr9686BF.js";
9
+ import { Command } from "commander";
10
+
11
+ //#region src/cli/bin.ts
12
+ const program = new Command();
13
+ program.name("db").description("Database migration management").version("0.0.1");
14
+ registerDbCommands(program);
15
+ program.parse();
16
+
17
+ //#endregion
18
+ export { };
@@ -0,0 +1,330 @@
1
+ import { t as Snapshot } from "../snapshot-C162qYoA.js";
2
+ import { l as DatabaseInfo } from "../defineDatabase-jBv90-Pk.js";
3
+ import "../index-BFM5d0FP.js";
4
+ import { Command } from "commander";
5
+
6
+ //#region src/cli/command.d.ts
7
+
8
+ /**
9
+ * Register all database subcommands (push, generate, status, reset,
10
+ * validate) directly on the given Commander command.
11
+ *
12
+ * Use this to flatten the commands onto your own program — the standalone
13
+ * `db` binary does this so `db push` works without a nested group:
14
+ *
15
+ * registerDbCommands(program); // → `mycli push`
16
+ *
17
+ * Or use {@link createDbCommand} to mount them as a `db` group:
18
+ *
19
+ * program.addCommand(createDbCommand()); // → `mycli db push`
20
+ */
21
+ declare function registerDbCommands(program: Command): Command;
22
+ /**
23
+ * Create a Commander command group named `db` with all database
24
+ * subcommands. Mount it in a host CLI with
25
+ * `program.addCommand(createDbCommand())` so the commands live under a
26
+ * `db` prefix (e.g. `mycli db push`).
27
+ */
28
+ declare function createDbCommand(): Command;
29
+ //#endregion
30
+ //#region src/cli/state.d.ts
31
+ interface DatabaseDevState {
32
+ /** Hash of the production snapshot when dev migrations started */
33
+ prodSnapshotHash: string;
34
+ /** Timestamp of last push */
35
+ lastPush: string | null;
36
+ }
37
+ interface DevState {
38
+ /** Epoch for instance key suffixing - bump this to reset all dev DBs */
39
+ epoch: string;
40
+ /** Per-database state */
41
+ databases: Record<string, DatabaseDevState>;
42
+ }
43
+ interface DevPaths {
44
+ /** Root cache directory */
45
+ cacheDir: string;
46
+ /** State file path */
47
+ stateFile: string;
48
+ /** Database-specific directory */
49
+ databaseDir: (dbName: string) => string;
50
+ /** Dev migrations directory for a database */
51
+ migrationsDir: (dbName: string) => string;
52
+ /** Dev snapshot file for a database */
53
+ snapshotFile: (dbName: string) => string;
54
+ }
55
+ /**
56
+ * Get all dev-related paths for a project
57
+ */
58
+ declare function getDevPaths(projectRoot: string): DevPaths;
59
+ /**
60
+ * Generate a new epoch value (timestamp-based)
61
+ */
62
+ declare function generateEpoch(): string;
63
+ /**
64
+ * Load the dev state from disk
65
+ */
66
+ declare function loadDevState(projectRoot: string): DevState;
67
+ /**
68
+ * Save the dev state to disk
69
+ */
70
+ declare function saveDevState(projectRoot: string, state: DevState): void;
71
+ /**
72
+ * Load dev migrations for a database
73
+ */
74
+ declare function loadDevMigrations(projectRoot: string, dbName: string): Map<string, string[][]>;
75
+ /**
76
+ * Get the dev epoch for instance key suffixing
77
+ * Returns null in production or if no dev state exists
78
+ */
79
+ declare function getDevEpoch(projectRoot: string): string | null;
80
+ /**
81
+ * Get a database instance key with optional dev epoch suffix
82
+ */
83
+ declare function getInstanceKey(baseKey: string, epoch: string | null): string;
84
+ //#endregion
85
+ //#region src/cli/push.d.ts
86
+ interface PushContext {
87
+ projectRoot?: string;
88
+ databasesDir?: string;
89
+ migrationsDir?: string;
90
+ verbose?: boolean;
91
+ }
92
+ interface PushResult {
93
+ database: string;
94
+ hasChanges: boolean;
95
+ statements: string[];
96
+ migrationName: string | null;
97
+ }
98
+ /**
99
+ * Push schema changes to dev migrations
100
+ *
101
+ * This command:
102
+ * 1. Discovers all databases in the project
103
+ * 2. For each database, diffs production snapshot against current schema
104
+ * 3. If changes detected, generates a single squashed dev migration
105
+ * 4. Uses content-hash naming so the same schema always produces the same
106
+ * migration name — the DO skips it if already applied
107
+ *
108
+ * Dev migrations are stored in node_modules/.cache/durable-db/
109
+ * and are ephemeral - they're cleared when running `db:generate` or `db:reset`
110
+ */
111
+ declare function push(ctx?: PushContext): Promise<PushResult[]>;
112
+ /**
113
+ * Push a single database by name
114
+ * Useful when you only want to push one specific database
115
+ */
116
+ declare function pushOne(ctx: PushContext | undefined, dbName: string): Promise<PushResult | null>;
117
+ //#endregion
118
+ //#region src/cli/generate.d.ts
119
+ interface GenerateContext {
120
+ projectRoot?: string;
121
+ databasesDir?: string;
122
+ migrationsDir?: string;
123
+ verbose?: boolean;
124
+ }
125
+ interface GenerateOptions {
126
+ /** Custom migration name suffix (e.g., 'add_user_bio') */
127
+ name?: string;
128
+ /** Only generate for this database */
129
+ database?: string;
130
+ }
131
+ interface GenerateResult {
132
+ database: string;
133
+ hasChanges: boolean;
134
+ statements: string[];
135
+ migrationName: string | null;
136
+ migrationPath: string | null;
137
+ }
138
+ /**
139
+ * Generate production migrations from schema changes
140
+ *
141
+ * This command:
142
+ * 1. Compares current schema to production snapshot
143
+ * 2. Generates a single migration file with all changes
144
+ * 3. Updates the production snapshot
145
+ * 4. Clears dev migrations (they're now consolidated)
146
+ *
147
+ * Unlike `push`, this writes to the actual migrations directory
148
+ * that gets committed to git.
149
+ */
150
+ declare function generate(ctx?: GenerateContext, options?: GenerateOptions): Promise<GenerateResult[]>;
151
+ /**
152
+ * Generate migration for a single database by name
153
+ */
154
+ declare function generateOne(ctx: GenerateContext, dbName: string, options?: Omit<GenerateOptions, 'database'>): Promise<GenerateResult | null>;
155
+ //#endregion
156
+ //#region src/cli/reset.d.ts
157
+ interface ResetContext {
158
+ projectRoot?: string;
159
+ databasesDir?: string;
160
+ migrationsDir?: string;
161
+ verbose?: boolean;
162
+ }
163
+ interface ResetOptions {
164
+ /** Only reset this database */
165
+ database?: string;
166
+ /** Don't bump the epoch (only clear dev migrations) */
167
+ keepEpoch?: boolean;
168
+ /**
169
+ * Also delete workerd's persisted DO storage under .wrangler/.
170
+ *
171
+ * Not needed for a fresh start — the epoch bump already rotates every
172
+ * database to a brand-new DO instance. This only reclaims the disk space
173
+ * of the orphaned instances, and requires the dev server to be stopped.
174
+ */
175
+ purgeLocalStorage?: boolean;
176
+ }
177
+ interface ResetResult {
178
+ /** New epoch value (null if keepEpoch was true) */
179
+ newEpoch: string | null;
180
+ /** Databases that were reset */
181
+ databases: string[];
182
+ /** Local DO storage directories that were deleted (under .wrangler/) */
183
+ clearedStorageDirs: string[];
184
+ }
185
+ /**
186
+ * Reset dev state and optionally bump the epoch
187
+ *
188
+ * This command:
189
+ * 1. Bumps the epoch (unless keepEpoch is true)
190
+ * 2. Clears all dev migrations
191
+ * 3. Clears dev snapshots
192
+ *
193
+ * When the epoch changes, all local DO instances will use a new
194
+ * instance key suffix, effectively giving you fresh databases.
195
+ *
196
+ * This is useful when:
197
+ * - You want to start fresh with a clean database
198
+ * - Your local DB is in a broken state
199
+ * - Someone else committed migrations and you pulled
200
+ */
201
+ declare function reset(ctx?: ResetContext, options?: ResetOptions): Promise<ResetResult>;
202
+ /**
203
+ * Reset everything and re-run push
204
+ * Convenience function that combines reset + push
205
+ */
206
+ declare function resetAndPush(ctx?: ResetContext, options?: ResetOptions): Promise<{
207
+ reset: ResetResult;
208
+ push: PushResult[];
209
+ }>;
210
+ //#endregion
211
+ //#region src/cli/status.d.ts
212
+ interface StatusContext {
213
+ projectRoot?: string;
214
+ databasesDir?: string;
215
+ migrationsDir?: string;
216
+ verbose?: boolean;
217
+ }
218
+ interface DatabaseStatus {
219
+ name: string;
220
+ /** Number of production migrations */
221
+ prodMigrationCount: number;
222
+ /** Number of dev migrations */
223
+ devMigrationCount: number;
224
+ /** Whether schema has uncommitted changes */
225
+ hasUncommittedChanges: boolean;
226
+ /** SQL statements that would be generated */
227
+ pendingStatements: string[];
228
+ /** Whether prod snapshot changed (needs reset) */
229
+ prodSnapshotChanged: boolean;
230
+ /** Current dev epoch */
231
+ epoch: string;
232
+ /** Last push timestamp */
233
+ lastPush: string | null;
234
+ }
235
+ interface StatusResult {
236
+ /** Current dev epoch */
237
+ epoch: string;
238
+ /** Per-database status */
239
+ databases: DatabaseStatus[];
240
+ }
241
+ /**
242
+ * Get the current status of all databases
243
+ *
244
+ * Shows:
245
+ * - Number of production migrations
246
+ * - Number of dev migrations
247
+ * - Whether there are uncommitted schema changes
248
+ * - What SQL would be generated
249
+ */
250
+ declare function status(ctx?: StatusContext): Promise<StatusResult>;
251
+ /**
252
+ * Format status for display
253
+ */
254
+ declare function formatStatus(result: StatusResult): string;
255
+ //#endregion
256
+ //#region src/cli/validate.d.ts
257
+ interface ValidateContext {
258
+ projectRoot?: string;
259
+ databasesDir?: string;
260
+ migrationsDir?: string;
261
+ verbose?: boolean;
262
+ /** Skip dev migrations, only validate prod */
263
+ noDev?: boolean;
264
+ /** Only validate this database */
265
+ database?: string;
266
+ }
267
+ interface ValidateError {
268
+ migration: string;
269
+ chunk: number;
270
+ statement: string;
271
+ error: string;
272
+ }
273
+ interface ValidateResult {
274
+ database: string;
275
+ migrationsValid: boolean;
276
+ errors: ValidateError[];
277
+ schemaMatches: boolean;
278
+ schemaDiffs: string[];
279
+ migrationCount: number;
280
+ includesDevMigrations: boolean;
281
+ }
282
+ /**
283
+ * Dry-run all migrations against a local in-memory SQLite to catch errors
284
+ * before deployment. Optionally compares the migrated schema against a
285
+ * from-scratch schema build (dual-path verification).
286
+ */
287
+ declare function validate(ctx?: ValidateContext): Promise<ValidateResult[]>;
288
+ //#endregion
289
+ //#region src/cli/shared.d.ts
290
+ /**
291
+ * Print a caught error to stderr. Always shows the message; when verbose
292
+ * (flag or `DEBUG=*`) is set, also prints the stack so users can diagnose
293
+ * unexpected failures without patching the CLI.
294
+ */
295
+ declare function reportCliError(error: unknown, verbose?: boolean): void;
296
+ interface DiscoverOptions {
297
+ projectRoot: string;
298
+ databasesDir: string;
299
+ migrationsDir: string;
300
+ /** Only return databases matching this name */
301
+ database?: string;
302
+ }
303
+ interface DiffResult {
304
+ hasChanges: boolean;
305
+ statements: string[];
306
+ prodSnapshot: Snapshot;
307
+ currentSnapshot: Snapshot;
308
+ }
309
+ /**
310
+ * Discover database files, parse them, and resolve migrationsDir.
311
+ * Returns parsed DatabaseInfo[] with migrationsDir already set.
312
+ */
313
+ declare function discoverDatabases(opts: DiscoverOptions): DatabaseInfo[];
314
+ /**
315
+ * Load and validate schema for a database.
316
+ *
317
+ * Returns null only when the database genuinely declares no schema tables.
318
+ * Every other failure mode (inline tables, unresolvable import, build error,
319
+ * missing exports) throws — silently skipping a database is how a typo in
320
+ * schema.ts becomes "why is my migration empty" 20 minutes later, or worse,
321
+ * a generated migration that DROPs tables that still exist in production.
322
+ */
323
+ declare function loadSchema(db: DatabaseInfo): Promise<Record<string, unknown> | null>;
324
+ /**
325
+ * Diff a schema against the production snapshot in migrationsDir.
326
+ * Returns whether there are changes, the SQL statements, and both snapshots.
327
+ */
328
+ declare function diffSchema(schema: Record<string, unknown>, migrationsDir: string): Promise<DiffResult>;
329
+ //#endregion
330
+ export { type DatabaseDevState, type DatabaseStatus, type DevPaths, type DevState, type DiffResult, type DiscoverOptions, type GenerateContext, type GenerateOptions, type GenerateResult, type PushContext, type PushResult, type ResetContext, type ResetOptions, type ResetResult, type StatusContext, type StatusResult, type ValidateContext, type ValidateError, type ValidateResult, createDbCommand, diffSchema, discoverDatabases, formatStatus, generate, generateEpoch, generateOne, getDevEpoch, getDevPaths, getInstanceKey, loadDevMigrations, loadDevState, loadSchema, push, pushOne, registerDbCommands, reportCliError, reset, resetAndPush, saveDevState, status, validate };
@@ -0,0 +1,9 @@
1
+ import "../debug-BZJbKybf.js";
2
+ import { _ as saveDevState, a as generateEpoch, c as getDevPaths, f as loadDevMigrations, l as getInstanceKey, m as loadDevState, s as getDevEpoch } from "../state-FOEjpgJ6.js";
3
+ import "../snapshot-D170nL7i.js";
4
+ import "../generator-BUPP9scJ.js";
5
+ import "../parser-CbX54Flt.js";
6
+ import { a as loadSchema, i as discoverDatabases, n as pushOne, o as reportCliError, r as diffSchema, t as push } from "../push-BgQ5cnfH.js";
7
+ import { a as resetAndPush, c as generate, i as reset, l as generateOne, n as registerDbCommands, o as formatStatus, r as validate, s as status, t as createDbCommand } from "../cli-Cr9686BF.js";
8
+
9
+ export { createDbCommand, diffSchema, discoverDatabases, formatStatus, generate, generateEpoch, generateOne, getDevEpoch, getDevPaths, getInstanceKey, loadDevMigrations, loadDevState, loadSchema, push, pushOne, registerDbCommands, reportCliError, reset, resetAndPush, saveDevState, status, validate };