import { PrismaClient } from "@prisma/client";

/**
 * Prisma Client 单例
 * - 开发环境：避免 Next.js HMR 时反复创建连接
 * - 生产环境：单例实例
 */

const globalForPrisma = globalThis as unknown as {
  prisma: PrismaClient | undefined;
};

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

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