fix: return dummy prisma proxy when DB_HOST is missing to prevent build-time connection pool timeouts

This commit is contained in:
Rio
2026-06-18 14:40:59 +07:00
parent e8d3cd804f
commit 47edd33923
+24 -4
View File
@@ -8,11 +8,31 @@ declare global {
// Guard: don't attempt a real DB connection at build time when env vars are absent.
// During `next build` inside Docker, DB_HOST is not injected — only available at runtime.
function createPrismaClient(connectionLimit: number): PrismaClient {
const host = process.env.DB_HOST || 'localhost';
if (!process.env.DB_HOST) {
// Return a proxy that immediately rejects all database calls to prevent build-time hangs.
const dummyPrisma = new Proxy({} as any, {
get(target, prop) {
if (prop === '$transaction') {
return (fn: any) => fn(dummyPrisma);
}
if (typeof prop === 'string' && prop.startsWith('$')) {
return () => Promise.reject(new Error('Database not available during build'));
}
return new Proxy({}, {
get(subTarget, subProp) {
return () => Promise.reject(new Error('Database not available during build'));
}
});
}
});
return dummyPrisma;
}
const host = process.env.DB_HOST;
const port = Number(process.env.DB_PORT) || 3306;
const user = process.env.DB_USERNAME || 'dummy';
const password = process.env.DB_PASSWORD || 'dummy';
const database = process.env.DB_NAME || 'dummy';
const user = process.env.DB_USERNAME;
const password = process.env.DB_PASSWORD;
const database = process.env.DB_NAME;
const adapter = new PrismaMariaDb({
host,