33 lines
975 B
TypeScript
33 lines
975 B
TypeScript
import { PrismaClient } from "@prisma/client";
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
|
|
|
declare global {
|
|
// eslint-disable-next-line no-var
|
|
var prismaGlobal: PrismaClient | undefined;
|
|
}
|
|
|
|
/**
|
|
* Rust-free Prisma client over node-postgres (no native engine binary needed,
|
|
* which also makes shared/VPS hosting simpler).
|
|
*/
|
|
export function createPrisma(url = process.env.DATABASE_URL) {
|
|
if (!url) throw new Error("DATABASE_URL is not set");
|
|
return new PrismaClient({ adapter: new PrismaPg({ connectionString: url }) });
|
|
}
|
|
|
|
function instance(): PrismaClient {
|
|
if (!global.prismaGlobal) global.prismaGlobal = createPrisma();
|
|
return global.prismaGlobal;
|
|
}
|
|
|
|
/** Lazily created so importing this module never needs DATABASE_URL (tests, build). */
|
|
const prisma = new Proxy({} as PrismaClient, {
|
|
get(_t, prop) {
|
|
const db = instance();
|
|
const v = Reflect.get(db, prop);
|
|
return typeof v === "function" ? v.bind(db) : v;
|
|
},
|
|
});
|
|
|
|
export default prisma;
|