diff --git a/server/app.ts b/server/app.ts index 4ba0fd7..c175623 100644 --- a/server/app.ts +++ b/server/app.ts @@ -13,7 +13,14 @@ export const app = express(); if (!process.env.DATABASE_URL) throw new Error("DATABASE_URL is required"); -const client = postgres(process.env.DATABASE_URL); +// In dev, ssrLoadModule re-evaluates this file on every request. Cache the +// client on globalThis so HMR re-loads reuse the same connection pool instead +// of leaking a new one each time. +declare global { + // TypeScript requires `var` in `declare global` blocks — `let`/`const` are not allowed here. + var __pgClient: ReturnType | undefined; // eslint-disable-line no-var +} +const client = (globalThis.__pgClient ??= postgres(process.env.DATABASE_URL)); const db = drizzle(client, { schema }); app.use((_, __, next) => DatabaseContext.run(db, next));