@hamur/db 0.0.1 → 0.0.3

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/dist/src/cli.js CHANGED
@@ -509,46 +509,63 @@ async function writeD1Migrations(hamur, options) {
509
509
  await writeFile(wranglerFile, `${JSON.stringify(wrangler, null, 2)}
510
510
  `);
511
511
  }
512
- async function watchMigrations(hamur, options) {
513
- const dirs = new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir));
514
- const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)];
515
- const roots = [...serverDirs, ...[...dirs].filter((dir) => !serverDirs.some((root) => dir.startsWith(root + sep)))];
512
+ function applyChanges(hamur, options, stale) {
513
+ const log = useLogger2("hamur");
516
514
  const watchers = [];
515
+ let closed = false;
517
516
  let timer;
518
517
  let running = Promise.resolve();
519
- const log = useLogger2("hamur");
520
518
  const changed = () => {
521
519
  clearTimeout(timer);
520
+ if (closed)
521
+ return;
522
522
  timer = setTimeout(() => {
523
523
  running = running.then(() => applyAll(hamur, options)).catch((error) => log.error(error instanceof Error ? error.message : String(error)));
524
524
  }, 100);
525
525
  };
526
- for (const root of roots) {
527
- try {
528
- const watcher = watch(root, { recursive: true }, (_event, file) => {
529
- if (typeof file === "string" && file.endsWith(".sql") && dirs.has(dirname(join3(root, file))))
530
- changed();
531
- });
532
- watcher.on("error", () => watcher.close());
533
- watchers.push(watcher);
534
- } catch {}
535
- }
536
- return () => {
526
+ const dirs = async () => new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir));
527
+ hamur.hooks.hook("dev:change", async ({ file }) => {
528
+ if (file.endsWith(".sql") && (await dirs()).has(dirname(file)))
529
+ changed();
530
+ });
531
+ hamur.hooks.hook("dev:ready", async () => {
532
+ const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)];
533
+ for (const dir of await dirs()) {
534
+ if (closed || serverDirs.some((root) => dir.startsWith(root + sep)))
535
+ continue;
536
+ try {
537
+ const watcher = watch(dir, (_event, file) => {
538
+ if (typeof file === "string" && file.endsWith(".sql"))
539
+ changed();
540
+ });
541
+ watcher.on("error", () => watcher.close());
542
+ watchers.push(watcher);
543
+ } catch {}
544
+ }
545
+ if (await stale())
546
+ changed();
547
+ });
548
+ hamur.hooks.hook("close", () => {
549
+ closed = true;
537
550
  clearTimeout(timer);
538
551
  for (const watcher of watchers)
539
552
  watcher.close();
540
- };
553
+ });
541
554
  }
542
555
  var setupMigrations = (options, hamur) => {
543
556
  const { dev, preset } = hamur.options;
544
557
  const atStart = options.migrate !== false && (dev || options.migrate === "start");
558
+ const current = async () => {
559
+ const all = await loadMigrations(hamur, options);
560
+ return JSON.stringify(Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []])));
561
+ };
562
+ let started;
545
563
  addTemplate2({
546
564
  filename: "db/migrations",
547
565
  getContents: async () => {
548
- const all = await loadMigrations(hamur, options);
549
- const migrations = Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []]));
566
+ started = await current();
550
567
  return `export const apply = ${atStart};
551
- export const migrations = ${JSON.stringify(migrations)};
568
+ export const migrations = ${started};
552
569
  `;
553
570
  }
554
571
  });
@@ -569,11 +586,13 @@ export const migrations = ${JSON.stringify(migrations)};
569
586
  hamur.hooks.hook("prerender:routes", () => applyDev(hamur, options));
570
587
  if (!dev && preset === "cloudflare")
571
588
  hamur.hooks.hook("build:done", () => writeD1Migrations(hamur, options));
572
- if (dev && options.migrate !== false) {
573
- let stop;
574
- hamur.hooks.hook("dev:ready", () => void (stop = watchMigrations(hamur, options)));
575
- hamur.hooks.hook("close", async () => (await stop)?.());
576
- }
589
+ if (dev && options.migrate !== false)
590
+ applyChanges(hamur, options, async () => {
591
+ const now = await current().catch(() => {
592
+ return;
593
+ });
594
+ return now !== started;
595
+ });
577
596
  };
578
597
 
579
598
  // packages/db/src/build/queue.ts
package/dist/src/index.js CHANGED
@@ -35,7 +35,7 @@ var __chunks = (base, ids, nodes, entry) => {
35
35
  __chunkSeen ||= {};
36
36
  __preload(ids[entry], 1);
37
37
  };
38
- __chunks(import.meta.url,["5cfxfqed","2yeva2qv","xxptm4db","a16d23f0","ehmpv0kf","3t2ajr9m","ppkx8k0t","kf697bfy"],[["./index.js",5],["../chunks/browser-bun-sql-2yeva2qv.js",6],["../chunks/browser-bun-sqlite-xxptm4db.js",6],["../chunks/browser-d1-a16d23f0.js",7],["../chunks/browser-client-ehmpv0kf.js",5,7,6],["../chunks/browser-index-3t2ajr9m.js"],["../chunks/browser-index-ppkx8k0t.js"],["../chunks/browser-index-kf697bfy.js",6]],0);
38
+ __chunks(import.meta.url,["sc99d8f1","2yeva2qv","xxptm4db","a16d23f0","ehmpv0kf","3t2ajr9m","ppkx8k0t","kf697bfy"],[["./index.js",5],["../chunks/browser-bun-sql-2yeva2qv.js",6],["../chunks/browser-bun-sqlite-xxptm4db.js",6],["../chunks/browser-d1-a16d23f0.js",7],["../chunks/browser-client-ehmpv0kf.js",5,7,6],["../chunks/browser-index-3t2ajr9m.js"],["../chunks/browser-index-ppkx8k0t.js"],["../chunks/browser-index-kf697bfy.js",6]],0);
39
39
 
40
40
  // packages/db/src/module.ts
41
41
  import { addDevtoolsTab, createResolver as createResolver3, defineModule } from "hamur/kit";
@@ -586,46 +586,63 @@ async function writeD1Migrations(hamur, options) {
586
586
  await writeFile(wranglerFile, `${JSON.stringify(wrangler, null, 2)}
587
587
  `);
588
588
  }
589
- async function watchMigrations(hamur, options) {
590
- const dirs = new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir));
591
- const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)];
592
- const roots = [...serverDirs, ...[...dirs].filter((dir) => !serverDirs.some((root) => dir.startsWith(root + sep)))];
589
+ function applyChanges(hamur, options, stale) {
590
+ const log = useLogger2("hamur");
593
591
  const watchers = [];
592
+ let closed = false;
594
593
  let timer;
595
594
  let running = Promise.resolve();
596
- const log = useLogger2("hamur");
597
595
  const changed = () => {
598
596
  clearTimeout(timer);
597
+ if (closed)
598
+ return;
599
599
  timer = setTimeout(() => {
600
600
  running = running.then(() => applyAll(hamur, options)).catch((error) => log.error(error instanceof Error ? error.message : String(error)));
601
601
  }, 100);
602
602
  };
603
- for (const root of roots) {
604
- try {
605
- const watcher = watch(root, { recursive: true }, (_event, file) => {
606
- if (typeof file === "string" && file.endsWith(".sql") && dirs.has(dirname(join3(root, file))))
607
- changed();
608
- });
609
- watcher.on("error", () => watcher.close());
610
- watchers.push(watcher);
611
- } catch {}
612
- }
613
- return () => {
603
+ const dirs = async () => new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir));
604
+ hamur.hooks.hook("dev:change", async ({ file }) => {
605
+ if (file.endsWith(".sql") && (await dirs()).has(dirname(file)))
606
+ changed();
607
+ });
608
+ hamur.hooks.hook("dev:ready", async () => {
609
+ const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)];
610
+ for (const dir of await dirs()) {
611
+ if (closed || serverDirs.some((root) => dir.startsWith(root + sep)))
612
+ continue;
613
+ try {
614
+ const watcher = watch(dir, (_event, file) => {
615
+ if (typeof file === "string" && file.endsWith(".sql"))
616
+ changed();
617
+ });
618
+ watcher.on("error", () => watcher.close());
619
+ watchers.push(watcher);
620
+ } catch {}
621
+ }
622
+ if (await stale())
623
+ changed();
624
+ });
625
+ hamur.hooks.hook("close", () => {
626
+ closed = true;
614
627
  clearTimeout(timer);
615
628
  for (const watcher of watchers)
616
629
  watcher.close();
617
- };
630
+ });
618
631
  }
619
632
  var setupMigrations = (options, hamur) => {
620
633
  const { dev, preset } = hamur.options;
621
634
  const atStart = options.migrate !== false && (dev || options.migrate === "start");
635
+ const current = async () => {
636
+ const all = await loadMigrations(hamur, options);
637
+ return JSON.stringify(Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []])));
638
+ };
639
+ let started;
622
640
  addTemplate2({
623
641
  filename: "db/migrations",
624
642
  getContents: async () => {
625
- const all = await loadMigrations(hamur, options);
626
- const migrations = Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []]));
643
+ started = await current();
627
644
  return `export const apply = ${atStart};
628
- export const migrations = ${JSON.stringify(migrations)};
645
+ export const migrations = ${started};
629
646
  `;
630
647
  }
631
648
  });
@@ -646,11 +663,13 @@ export const migrations = ${JSON.stringify(migrations)};
646
663
  hamur.hooks.hook("prerender:routes", () => applyDev(hamur, options));
647
664
  if (!dev && preset === "cloudflare")
648
665
  hamur.hooks.hook("build:done", () => writeD1Migrations(hamur, options));
649
- if (dev && options.migrate !== false) {
650
- let stop;
651
- hamur.hooks.hook("dev:ready", () => void (stop = watchMigrations(hamur, options)));
652
- hamur.hooks.hook("close", async () => (await stop)?.());
653
- }
666
+ if (dev && options.migrate !== false)
667
+ applyChanges(hamur, options, async () => {
668
+ const now = await current().catch(() => {
669
+ return;
670
+ });
671
+ return now !== started;
672
+ });
654
673
  };
655
674
 
656
675
  // packages/db/src/build/schema.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hamur/db",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -11,23 +11,38 @@
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./dist/types/src/index.d.ts",
14
- "default": "./dist/src/index.js"
14
+ "bun": "./src/index.ts",
15
+ "hamur-source": "./src/index.ts",
16
+ "node": "./dist/src/index.js",
17
+ "default": "./src/index.ts"
15
18
  },
16
19
  "./server": {
17
20
  "types": "./dist/types/src/server/index.d.ts",
18
- "default": "./dist/src/server/index.js"
21
+ "bun": "./src/server/index.ts",
22
+ "hamur-source": "./src/server/index.ts",
23
+ "node": "./dist/src/server/index.js",
24
+ "default": "./src/server/index.ts"
19
25
  },
20
26
  "./storage": {
21
27
  "types": "./dist/types/src/storage/define.d.ts",
22
- "default": "./dist/src/storage/define.js"
28
+ "bun": "./src/storage/define.ts",
29
+ "hamur-source": "./src/storage/define.ts",
30
+ "node": "./dist/src/storage/define.js",
31
+ "default": "./src/storage/define.ts"
23
32
  },
24
33
  "./cli": {
25
34
  "types": "./dist/types/src/cli.d.ts",
26
- "default": "./dist/src/cli.js"
35
+ "bun": "./src/cli.ts",
36
+ "hamur-source": "./src/cli.ts",
37
+ "node": "./dist/src/cli.js",
38
+ "default": "./src/cli.ts"
27
39
  },
28
40
  "./devtools": {
29
41
  "types": "./dist/types/src/devtools.d.ts",
30
- "default": "./dist/src/devtools.js"
42
+ "bun": "./src/devtools.ts",
43
+ "hamur-source": "./src/devtools.ts",
44
+ "node": "./dist/src/devtools.js",
45
+ "default": "./src/devtools.ts"
31
46
  },
32
47
  "./package.json": "./package.json"
33
48
  },
@@ -37,6 +52,6 @@
37
52
  ],
38
53
  "dependencies": {},
39
54
  "peerDependencies": {
40
- "hamur": "~0.0.1"
55
+ "hamur": "~0.0.3"
41
56
  }
42
57
  }
@@ -169,38 +169,52 @@ async function writeD1Migrations(hamur: HamurContext, options: ResolvedDbOptions
169
169
  await writeFile(wranglerFile, `${JSON.stringify(wrangler, null, 2)}\n`)
170
170
  }
171
171
 
172
- /** Under `hamur dev`, a migration file added or changed while it runs is applied at once; an error is logged, the server keeps running. */
173
- async function watchMigrations(hamur: HamurContext, options: ResolvedDbOptions): Promise<() => void> {
174
- const dirs = new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir))
175
- const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)]
176
- const roots = [...serverDirs, ...[...dirs].filter((dir) => !serverDirs.some((root) => dir.startsWith(root + sep)))]
172
+ /**
173
+ * Under `hamur dev`, a migration file added or changed while it runs is applied at once; an error is logged, the server
174
+ * keeps running. `stale` answers whether the files differ from those the server's start applied.
175
+ */
176
+ function applyChanges(hamur: HamurContext, options: ResolvedDbOptions, stale: () => Promise<boolean>): void {
177
+ const log = useLogger("hamur")
177
178
  const watchers: FSWatcher[] = []
179
+ let closed = false
178
180
  let timer: ReturnType<typeof setTimeout> | undefined
179
181
  let running = Promise.resolve()
180
- const log = useLogger("hamur")
181
182
  const changed = () => {
182
183
  clearTimeout(timer)
184
+ if (closed) return
183
185
  timer = setTimeout(() => {
184
186
  running = running
185
187
  .then(() => applyAll(hamur, options))
186
188
  .catch((error: unknown) => log.error(error instanceof Error ? error.message : String(error)))
187
189
  }, 100)
188
190
  }
189
- for (const root of roots) {
190
- try {
191
- const watcher = watch(root, { recursive: true }, (_event, file) => {
192
- if (typeof file === "string" && file.endsWith(".sql") && dirs.has(dirname(join(root, file)))) changed()
193
- })
194
- watcher.on("error", () => watcher.close())
195
- watchers.push(watcher)
196
- } catch {
197
- // A directory that does not exist has nothing to watch.
191
+ const dirs = async () => new Set((await migrationDirs(hamur, options)).map((entry) => entry.dir))
192
+
193
+ hamur.hooks.hook("dev:change", async ({ file }) => {
194
+ if (file.endsWith(".sql") && (await dirs()).has(dirname(file))) changed()
195
+ })
196
+ hamur.hooks.hook("dev:ready", async () => {
197
+ // `dev:change` covers the server directories; a module's directory may be in node_modules, which it leaves out.
198
+ const serverDirs = [hamur.options.serverDir, ...hamur.options.layers.map((layer) => layer.serverDir)]
199
+ for (const dir of await dirs()) {
200
+ if (closed || serverDirs.some((root) => dir.startsWith(root + sep))) continue
201
+ try {
202
+ const watcher = watch(dir, (_event, file) => {
203
+ if (typeof file === "string" && file.endsWith(".sql")) changed()
204
+ })
205
+ watcher.on("error", () => watcher.close())
206
+ watchers.push(watcher)
207
+ } catch {
208
+ // A directory that does not exist has nothing to watch.
209
+ }
198
210
  }
199
- }
200
- return () => {
211
+ if (await stale()) changed()
212
+ })
213
+ hamur.hooks.hook("close", () => {
214
+ closed = true
201
215
  clearTimeout(timer)
202
216
  for (const watcher of watchers) watcher.close()
203
- }
217
+ })
204
218
  }
205
219
 
206
220
  /**
@@ -210,12 +224,16 @@ async function watchMigrations(hamur: HamurContext, options: ResolvedDbOptions):
210
224
  export const setupMigrations: DbSetup = (options, hamur) => {
211
225
  const { dev, preset } = hamur.options
212
226
  const atStart = options.migrate !== false && (dev || options.migrate === "start")
227
+ const current = async () => {
228
+ const all = await loadMigrations(hamur, options)
229
+ return JSON.stringify(Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []])))
230
+ }
231
+ let started: string | undefined
213
232
  addTemplate({
214
233
  filename: "db/migrations",
215
234
  getContents: async () => {
216
- const all = await loadMigrations(hamur, options)
217
- const migrations = Object.fromEntries(notD1(options).map((name) => [name, all[name] ?? []]))
218
- return `export const apply = ${atStart};\nexport const migrations = ${JSON.stringify(migrations)};\n`
235
+ started = await current()
236
+ return `export const apply = ${atStart};\nexport const migrations = ${started};\n`
219
237
  },
220
238
  })
221
239
  addTask({ name: "db:migrate", file: createResolver(import.meta.url).resolve("../server/task.ts") })
@@ -239,9 +257,9 @@ export const setupMigrations: DbSetup = (options, hamur) => {
239
257
  if (!dev && options.migrate === "build") hamur.hooks.hook("prerender:routes", () => applyAll(hamur, options))
240
258
  if (!dev) hamur.hooks.hook("prerender:routes", () => applyDev(hamur, options))
241
259
  if (!dev && preset === "cloudflare") hamur.hooks.hook("build:done", () => writeD1Migrations(hamur, options))
242
- if (dev && options.migrate !== false) {
243
- let stop: Promise<() => void> | undefined
244
- hamur.hooks.hook("dev:ready", () => void (stop = watchMigrations(hamur, options)))
245
- hamur.hooks.hook("close", async () => (await stop)?.())
246
- }
260
+ if (dev && options.migrate !== false)
261
+ applyChanges(hamur, options, async () => {
262
+ const now = await current().catch(() => undefined)
263
+ return now !== started
264
+ })
247
265
  }