mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-07-14 18:15:56 -04:00
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:
@@ -8,6 +8,7 @@ node_modules
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
data
|
||||
.DS_Store
|
||||
.vercel
|
||||
coverage
|
||||
|
||||
+26
-11
@@ -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);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user