feat: 项目初始化
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
||||
import { PrismaClient } from "../../prisma/generated/client";
|
||||
|
||||
const createMariaDbConfig = () => {
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
|
||||
if (!databaseUrl) {
|
||||
throw new Error("DATABASE_URL is not set.");
|
||||
}
|
||||
|
||||
const url = new URL(databaseUrl);
|
||||
|
||||
return {
|
||||
host: url.hostname,
|
||||
port: Number(url.port || 3306),
|
||||
user: decodeURIComponent(url.username),
|
||||
password: decodeURIComponent(url.password),
|
||||
database: decodeURIComponent(url.pathname.slice(1)),
|
||||
connectionLimit: 5,
|
||||
connectTimeout: 15_000,
|
||||
acquireTimeout: 20_000,
|
||||
};
|
||||
};
|
||||
|
||||
const prismaClientSingleton = () => {
|
||||
const adapter = new PrismaMariaDb(createMariaDbConfig());
|
||||
return new PrismaClient({ adapter });
|
||||
};
|
||||
|
||||
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClientSingleton | undefined;
|
||||
};
|
||||
|
||||
export const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
||||
Reference in New Issue
Block a user