/**
 * Prisma client singleton.
 *
 * Prisma's client is a connection-pooled datasource; constructing it per-query
 * exhausts connections. This module exports one shared instance per process.
 * Hot-reload (tsx watch) can re-import this module — the global guard prevents
 * a second PrismaClient from being created.
 */
import { PrismaClient } from "@prisma/client";

const globalForPrisma = globalThis as unknown as { __hubPrisma?: PrismaClient };

export const prisma: PrismaClient =
  globalForPrisma.__hubPrisma ??
  new PrismaClient({
    log: process.env["HUB_PRISMA_LOG"] !== undefined ? ["query", "error", "warn"] : ["error", "warn"],
  });

if (process.env["NODE_ENV"] !== "production") {
  globalForPrisma.__hubPrisma = prisma;
}
