@hamur/db 0.0.2 → 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 +44 -25
- package/dist/src/index.js +45 -26
- package/package.json +2 -2
- package/src/build/migrations.ts +44 -26
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
|
-
|
|
513
|
-
const
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
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
|
-
|
|
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 = ${
|
|
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
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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,["
|
|
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
|
-
|
|
590
|
-
const
|
|
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
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
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
|
-
|
|
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 = ${
|
|
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
|
-
|
|
651
|
-
|
|
652
|
-
|
|
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.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -52,6 +52,6 @@
|
|
|
52
52
|
],
|
|
53
53
|
"dependencies": {},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"hamur": "~0.0.
|
|
55
|
+
"hamur": "~0.0.3"
|
|
56
56
|
}
|
|
57
57
|
}
|
package/src/build/migrations.ts
CHANGED
|
@@ -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
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
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
|
-
|
|
217
|
-
const
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
}
|