mirror of
https://github.com/R0m1k3/PleinR.git
synced 2026-10-23 07:08:05 +02:00
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:
1 file changed
+37
-14
+37
-14
@@ -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 { Pool } from "pg";
|
||||||
import * as schema from "./schema";
|
import * as schema from "./schema";
|
||||||
|
|
||||||
const connectionString = process.env.DATABASE_URL;
|
// Lazily initialise the pool + drizzle client so that merely *importing* this
|
||||||
if (!connectionString) {
|
// module never requires DATABASE_URL. This matters during `next build`, which
|
||||||
throw new Error("DATABASE_URL is not set");
|
// 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.
|
function getPool(): Pool {
|
||||||
const globalForDb = globalThis as unknown as { __pleinrPool?: Pool };
|
if (globalForDb.__pleinrPool) return globalForDb.__pleinrPool;
|
||||||
|
const connectionString = process.env.DATABASE_URL;
|
||||||
export const pool =
|
if (!connectionString) {
|
||||||
globalForDb.__pleinrPool ??
|
throw new Error("DATABASE_URL is not set");
|
||||||
new Pool({
|
}
|
||||||
|
const pool = new Pool({
|
||||||
connectionString,
|
connectionString,
|
||||||
max: Number(process.env.PG_POOL_MAX ?? 10),
|
max: Number(process.env.PG_POOL_MAX ?? 10),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
|
||||||
globalForDb.__pleinrPool = pool;
|
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 };
|
export { schema };
|
||||||
Reference in new issue
Block a user