1type CachedConn = { destroy: () => Promise<void> } 3// Hoist to globalThis so all webpack module instances (Next.js dev recompiles 4// per-route + HMR) share one knex/kysely pool cache. Without this, each module 5// instance gets a fresh {} and orphaned pools leak real PG connections until 6// process exit — the #1 cause of dev pool exhaustion. 7const g = globalThis as unknown as { __dbConnCache?: Record<string, CachedConn> } 8export const existingConnsH: Record<string, CachedConn> = g.__dbConnCache ||= {} 10export const clearAllCachedConnections = () => { 11 for (const key of Object.keys(existingConnsH)) { 12 existingConnsH[key].destroy().catch(() => {}) 13 delete existingConnsH[key]