collect-system/packages/common/src/db.ts

18 lines
415 B
TypeScript
Raw Normal View History

2024-07-22 09:57:25 +08:00
import { PrismaClient } from "@prisma/client";
2024-12-31 15:57:32 +08:00
2024-07-22 09:57:25 +08:00
let prisma: PrismaClient | null = null;
2024-12-31 15:57:32 +08:00
2024-07-22 09:57:25 +08:00
const createPrismaClient = () => {
return new PrismaClient({
log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"],
});
};
2024-12-31 15:57:32 +08:00
2024-07-22 09:57:25 +08:00
export const db = (() => {
2024-12-30 08:26:40 +08:00
if (typeof window === 'undefined') {
if (!prisma) {
prisma = createPrismaClient();
}
return prisma;
2024-07-22 09:57:25 +08:00
}
2024-12-31 15:57:32 +08:00
})() as PrismaClient;