durable-db 0.0.1 → 0.0.2

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Zero-configuration database abstraction for Cloudflare Durable Objects with SQLite.
4
4
 
5
- Define your schema with [Drizzle](https://orm.drizzle.team/), query with [Kysely](https://kysely.dev/), validate with [ArkType](https://arktype.io/) -- and let the Vite plugin handle the rest: Durable Object class generation, RPC stubs, migration embedding, and wrangler config patching.
5
+ Define your schema with [Drizzle](https://orm.drizzle.team/), query with [Kysely](https://kysely.dev/), validate with [ArkType](https://arktype.io/) -- and let the Vite plugin handle the rest: Durable Object class generation, RPC stubs, migration embedding, and wrangler config verification (with opt-in auto-patching).
6
6
 
7
7
  ## Table of Contents
8
8
 
@@ -693,6 +693,7 @@ durableDb({
693
693
  | `migrationsDir` | `string` | `'migrations'` | Directory for production migrations, relative to project root. Each database gets a subdirectory (e.g. `migrations/main/`) |
694
694
  | `contextImport` | `string` | `'durable-db/context'` | Import path for the context module (for framework integrations) |
695
695
  | `registryImport` | `string` | `'durable-db/registry'` | Import path for the action registry module (for framework integrations) |
696
+ | `patchWranglerConfig` | `boolean` | `false` | Write missing DO bindings and sqlite migration entries to `wrangler.jsonc` automatically. Off by default — the plugin only verifies the config and logs the exact JSON to add. Only `wrangler.jsonc`/`wrangler.json` are supported (not `wrangler.toml`) |
696
697
 
697
698
  ### What the plugin does
698
699
 
@@ -701,9 +702,28 @@ durableDb({
701
702
  3. **Migration Loading**: Loads production migrations from disk; in dev mode also loads dev migrations from cache
702
703
  4. **Code Generation**: Produces a virtual module (`virtual:durable-db/__durableObjects`) containing Durable Object classes with embedded migrations and RPC dispatch methods, plus a `virtual:durable-db/__devEpoch` module that suffixes DO instance keys with the dev epoch (identity in production builds)
703
704
  5. **Action Transform**: Replaces `action()` call-sites with RPC stubs + `registerAction()` calls so actions can be called like regular functions from your worker. This applies to actions in separate files *and* actions defined in the database file itself
704
- 6. **Wrangler Patching**: Automatically updates `wrangler.jsonc` with Durable Object bindings and SQLite migration entries
705
+ 6. **Wrangler Verification**: Checks that `wrangler.jsonc` contains the required Durable Object bindings and SQLite migration entries, and logs the exact JSON to add when something is missing. With `patchWranglerConfig: true`, writes them for you instead
705
706
  7. **HMR**: Watches database files and invalidates the virtual module on change
706
707
 
708
+ ### Wrangler config: verify vs. patch
709
+
710
+ The plugin needs each database's Durable Object binding and a `new_sqlite_classes` migration entry in your wrangler config.
711
+
712
+ **Default (verify-only)**: your config is never modified. On startup the plugin checks for missing entries and logs the exact JSON to add, e.g.:
713
+
714
+ ```
715
+ [durable-db] wrangler.jsonc is missing Durable Object config for your database(s). Add this:
716
+ {
717
+ "durable_objects": { "bindings": [{ "name": "MAIN_DATABASE_DO", "class_name": "MainDatabaseDO" }] },
718
+ "migrations": [{ "tag": "v1", "new_sqlite_classes": ["MainDatabaseDO"] }]
719
+ }
720
+ Or let the plugin write it for you: durableDb({ patchWranglerConfig: true })
721
+ ```
722
+
723
+ **Opt-in (auto-patch)**: pass `durableDb({ patchWranglerConfig: true })` and the plugin writes the missing entries for you. If your `wrangler.jsonc` contains comments, the original is backed up to `wrangler.jsonc.backup` first (re-serializing strips comments).
724
+
725
+ > ⚠️ Only `wrangler.jsonc` / `wrangler.json` are supported. If your project uses `wrangler.toml`, the plugin can't verify or patch it — it will print the required config and you add the TOML equivalent yourself.
726
+
707
727
  ### Generated Class Naming
708
728
 
709
729
  For each `defineDatabase()` call, the plugin generates a class based on the filename:
@@ -10,6 +10,13 @@ interface DurableDbOptions {
10
10
  databasesDir?: string;
11
11
  /** Directory for production migrations, relative to project root. Default: 'migrations' */
12
12
  migrationsDir?: string;
13
+ /**
14
+ * Write missing Durable Object bindings and sqlite migration entries to
15
+ * wrangler.jsonc/json automatically. When false (the default), the plugin
16
+ * only verifies the config and logs the exact JSON to add when something
17
+ * is missing — your config file is never modified.
18
+ */
19
+ patchWranglerConfig?: boolean;
13
20
  }
14
21
  declare function durableDb(options?: DurableDbOptions): Plugin;
15
22
  /** @deprecated Renamed to {@link durableDb}. */
@@ -201,6 +201,65 @@ function logRequiredConfig(databases) {
201
201
  migrations
202
202
  });
203
203
  }
204
+ /**
205
+ * Verify (without writing) that the wrangler config contains the Durable
206
+ * Object bindings and sqlite migration entries the discovered databases
207
+ * need. Logs an actionable warning with the exact JSON to add when
208
+ * something is missing.
209
+ *
210
+ * This is the default behavior of the Vite plugin — automatic patching
211
+ * is opt-in via the `patchWranglerConfig` plugin option.
212
+ */
213
+ function checkWranglerConfig(projectRoot, databases) {
214
+ const required = generateRequiredConfig(databases);
215
+ const configPath = findWranglerConfig(projectRoot);
216
+ const result = {
217
+ configPath,
218
+ missingBindings: [],
219
+ missingSqliteClasses: [],
220
+ ok: true
221
+ };
222
+ let config = {};
223
+ if (configPath) try {
224
+ config = parseWranglerConfig(fs.readFileSync(configPath, "utf-8"));
225
+ } catch (error) {
226
+ debugVite("Failed to parse wrangler config for check: %O", error);
227
+ }
228
+ const existingBindings = config.durable_objects?.bindings ?? [];
229
+ result.missingBindings = (required.durable_objects?.bindings ?? []).filter((req) => !existingBindings.some((b) => b.name === req.name || b.class_name === req.class_name));
230
+ const existingClasses = new Set((config.migrations ?? []).flatMap((m) => m.new_sqlite_classes ?? []));
231
+ result.missingSqliteClasses = (required.migrations?.[0]?.new_sqlite_classes ?? []).filter((c) => !existingClasses.has(c));
232
+ result.ok = result.missingBindings.length === 0 && result.missingSqliteClasses.length === 0;
233
+ if (!result.ok) {
234
+ const snippet = JSON.stringify({
235
+ ...result.missingBindings.length > 0 && { durable_objects: { bindings: result.missingBindings } },
236
+ ...result.missingSqliteClasses.length > 0 && { migrations: [{
237
+ tag: "v1",
238
+ new_sqlite_classes: result.missingSqliteClasses
239
+ }] }
240
+ }, null, 2);
241
+ const hasToml = !configPath && fs.existsSync(path.join(projectRoot, "wrangler.toml"));
242
+ const where = configPath ? path.basename(configPath) : hasToml ? "wrangler.toml (not supported — only wrangler.jsonc/json can be verified or patched)" : "wrangler.jsonc";
243
+ console.warn(`[durable-db] ${where} is missing Durable Object config for your database(s). Add ${hasToml ? "the equivalent of " : ""}this${configPath || hasToml ? "" : " to a new wrangler.jsonc"}:\n${snippet}\n` + (hasToml ? "" : `Or let the plugin write it for you: durableDb({ patchWranglerConfig: true })`));
244
+ }
245
+ return result;
246
+ }
247
+ /**
248
+ * Generate the required wrangler config as a string
249
+ * Useful for displaying to users or tests
250
+ */
251
+ function generateRequiredConfig(databases) {
252
+ return {
253
+ durable_objects: { bindings: databases.map((db) => ({
254
+ name: db.bindingName,
255
+ class_name: db.className
256
+ })) },
257
+ migrations: [{
258
+ tag: "v1",
259
+ new_sqlite_classes: databases.map((db) => db.className)
260
+ }]
261
+ };
262
+ }
204
263
 
205
264
  //#endregion
206
265
  //#region src/vite/modules/generator.ts
@@ -748,7 +807,9 @@ var PluginState = class {
748
807
  }
749
808
  if (this.databases.size > 0) {
750
809
  debugVite("Found %d database(s)", this.databases.size);
751
- patchWranglerConfig(this.projectRoot, Array.from(this.databases.values()));
810
+ const databases = Array.from(this.databases.values());
811
+ if (this.options.patchWranglerConfig) patchWranglerConfig(this.projectRoot, databases);
812
+ else checkWranglerConfig(this.projectRoot, databases);
752
813
  }
753
814
  this.initialized = true;
754
815
  }
@@ -816,7 +877,8 @@ function durableDb(options = {}) {
816
877
  contextImport: options.contextImport ?? "durable-db/context",
817
878
  registryImport: options.registryImport ?? "durable-db/registry",
818
879
  databasesDir: options.databasesDir ?? "src/databases",
819
- migrationsDir: options.migrationsDir ?? "migrations"
880
+ migrationsDir: options.migrationsDir ?? "migrations",
881
+ patchWranglerConfig: options.patchWranglerConfig ?? false
820
882
  });
821
883
  return {
822
884
  name: "durable-db",
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "durable-db",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/eagerpatch/durable-db.git"
7
7
  },
8
8
  "type": "module",
9
- "packageManager": "pnpm@10.2.1",
10
9
  "bin": {
11
10
  "db": "./dist/cli/bin.js"
12
11
  },
@@ -66,13 +65,6 @@
66
65
  "publishConfig": {
67
66
  "access": "public"
68
67
  },
69
- "scripts": {
70
- "build": "tsdown",
71
- "dev": "tsdown --watch",
72
- "test": "vitest",
73
- "test:run": "vitest run",
74
- "changeset": "changeset"
75
- },
76
68
  "dependencies": {
77
69
  "@babel/generator": "^7.0.0",
78
70
  "@babel/parser": "^7.0.0",
@@ -110,5 +102,12 @@
110
102
  "typescript": "^5.7.0",
111
103
  "vite": "^7.0.0",
112
104
  "vitest": "^2.1.0"
105
+ },
106
+ "scripts": {
107
+ "build": "tsdown",
108
+ "dev": "tsdown --watch",
109
+ "test": "vitest",
110
+ "test:run": "vitest run",
111
+ "changeset": "changeset"
113
112
  }
114
- }
113
+ }