Corrige l'échec du build Docker : initialisation paresseuse du client DB

`next build` collecte les données de page en important les modules serveur sans
DATABASE_URL ; l'ancien client jetait une erreur dès l'import. Le client Drizzle
est désormais initialisé paresseusement (via un Proxy) à la première requête,
donc l'import est sans effet de bord et le build n'exige plus DATABASE_URL.
Vérifié : build OK sans variables d'env, et requêtes runtime OK via le proxy.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XwfhYHzC9hQWPDnQ2p53GL
This commit is contained in:
Claude committed 2026-06-26 16:35:38 +00:00
1 parent 718a704868
commit 71fb0ec032
1 file changed
+37 -14
+37 -14
View File
@@ -1,25 +1,48 @@
import { drizzle } from "drizzle-orm/node-postgres";
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
import * as schema from "./schema";
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
throw new Error("DATABASE_URL is not set");
}
// Lazily initialise the pool + drizzle client so that merely *importing* this
// module never requires DATABASE_URL. This matters during `next build`, which
// collects page data by importing server modules without runtime env set.
const globalForDb = globalThis as unknown as {
__pleinrPool?: Pool;
__pleinrDb?: NodePgDatabase<typeof schema>;
};
// Reuse the pool across hot reloads in dev to avoid exhausting connections.
const globalForDb = globalThis as unknown as { __pleinrPool?: Pool };
export const pool =
globalForDb.__pleinrPool ??
new Pool({
function getPool(): Pool {
if (globalForDb.__pleinrPool) return globalForDb.__pleinrPool;
const connectionString = process.env.DATABASE_URL;
if (!connectionString) {
throw new Error("DATABASE_URL is not set");
}
const pool = new Pool({
connectionString,
max: Number(process.env.PG_POOL_MAX ?? 10),
});
if (process.env.NODE_ENV !== "production") {
globalForDb.__pleinrPool = pool;
return pool;
}
function getDb(): NodePgDatabase<typeof schema> {
if (globalForDb.__pleinrDb) return globalForDb.__pleinrDb;
const instance = drizzle(getPool(), { schema });
globalForDb.__pleinrDb = instance;
return instance;
}
// Proxy that defers initialisation until the first property access (i.e. the
// first query at request time), so imports stay side-effect free.
export const db = new Proxy({} as NodePgDatabase<typeof schema>, {
get(_target, prop, receiver) {
const instance = getDb();
const value = Reflect.get(instance as object, prop, receiver);
return typeof value === "function" ? value.bind(instance) : value;
},
});
export function getPgPool(): Pool {
return getPool();
}
export const db = drizzle(pool, { schema });
export { schema };