Lazy-init DB client to fix Docker build failure

The DB client was initialized at import time, which caused Next.js page
data collection to fail during Docker builds where no database exists.
Wrapping in Proxy defers createClient() until first actual use.

Also adds data/ to .dockerignore.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-03 13:21:00 -05:00
co-authored by Claude Opus 4.6
parent 64351cb896
commit 0d272700f9
2 changed files with 27 additions and 11 deletions
+1
View File
@@ -8,6 +8,7 @@ node_modules
.env
.env.*
!.env.example
data
.DS_Store
.vercel
coverage
+26 -11
View File
@@ -7,18 +7,33 @@ const globalForDb = globalThis as unknown as {
_client: ReturnType<typeof createClient> | undefined;
};
if (!globalForDb._client) {
globalForDb._client = createClient({
url: process.env.DATABASE_URL ?? "file:./data/sqlite.db",
});
globalForDb._client.execute("PRAGMA journal_mode = WAL");
globalForDb._client.execute("PRAGMA foreign_keys = ON");
globalForDb._client.execute("PRAGMA busy_timeout = 5000");
function getClient() {
if (!globalForDb._client) {
globalForDb._client = createClient({
url: process.env.DATABASE_URL ?? "file:./data/sqlite.db",
});
globalForDb._client.execute("PRAGMA journal_mode = WAL");
globalForDb._client.execute("PRAGMA foreign_keys = ON");
globalForDb._client.execute("PRAGMA busy_timeout = 5000");
}
return globalForDb._client;
}
if (!globalForDb._db) {
globalForDb._db = drizzle({ client: globalForDb._client, schema });
function getDb() {
if (!globalForDb._db) {
globalForDb._db = drizzle({ client: getClient(), schema });
}
return globalForDb._db;
}
export const db = globalForDb._db;
export const client = globalForDb._client;
export const db = new Proxy({} as ReturnType<typeof drizzle>, {
get(_, prop) {
return Reflect.get(getDb(), prop);
},
});
export const client = new Proxy({} as ReturnType<typeof createClient>, {
get(_, prop) {
return Reflect.get(getClient(), prop);
},
});