-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind-local-d1.ts
More file actions
37 lines (32 loc) · 1021 Bytes
/
find-local-d1.ts
File metadata and controls
37 lines (32 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { readdirSync, statSync } from "fs";
import { join } from "path";
const D1_DIR = ".wrangler/state/v3/d1/miniflare-D1DatabaseObject";
export function findLocalD1Database(): string | null {
try {
const files = readdirSync(D1_DIR)
.filter((file) => file.endsWith(".sqlite") && file !== "metadata.sqlite")
.map((file) => ({
name: file,
path: join(D1_DIR, file),
time: statSync(join(D1_DIR, file)).mtime.getTime(),
}))
.sort((a, b) => b.time - a.time); // Sort by most recently modified
if (files.length === 0) {
console.error("No local D1 database found.");
console.error("Try running your dev server first: bun run dev");
return null;
}
if (files.length > 1) {
console.warn(
"Multiple D1 database files found. Using the most recent one.",
);
}
return files[0].path;
} catch (error) {
console.error("Error finding local D1 database:", error);
console.error(
"Make sure .wrangler directory exists. Try running: bun run dev",
);
return null;
}
}