From c42d06667c2d9f6c58f477e5a601f581bcebd23b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20V=C4=83n=20Rum=20=28VSF-QTVHNTDVX-PMGTCC?= =?UTF-8?q?=29?= Date: Mon, 8 Jun 2026 23:34:59 +0700 Subject: [PATCH] fix(prisma): construct pg adapter lazily so `next build` works without DATABASE_URL lib/prisma.ts threw `DATABASE_URL is not set` at module import. `next build` imports route modules to collect page data, so the Docker image build (which has no DATABASE_URL at build time) crashed with "Failed to collect page data". Prisma 6's PrismaClient was lazy and didn't throw at import. Drop the eager throw and pass the connection string straight to PrismaPg; the pg pool connects lazily on first query, so import-time construction is safe during the build. A missing URL now surfaces as a clear error on first query. --- lib/prisma.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index 7305975..ee79e31 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -5,15 +5,13 @@ const globalForPrisma = globalThis as unknown as { prisma: PrismaClient | undefined; }; -const connectionString = process.env.DATABASE_URL; - -if (!connectionString) { - throw new Error('DATABASE_URL is not set'); -} - // Prisma 7 connects through a driver adapter instead of a schema-level `url`. -// PrismaPg manages its own pg connection pool from the connection string. -const adapter = new PrismaPg({ connectionString }); +// PrismaPg manages its own pg connection pool. The pool connects lazily (on the +// first query), so constructing it at import time is safe even when +// DATABASE_URL is unset — e.g. during `next build`, which imports route modules +// to collect page data. A missing URL surfaces as a clear error on first query +// rather than crashing the build. +const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL }); export const prisma = globalForPrisma.prisma ??