2023-03-28 09:40:13 +00:00
import type { Prisma } from "@prisma/client" ;
2023-08-24 09:14:10 +00:00
import { PrismaClient as PrismaClientWithoutExtension } from "@prisma/client" ;
2023-09-19 21:02:57 +00:00
import { withAccelerate } from "@prisma/extension-accelerate" ;
2022-02-15 20:30:52 +00:00
2023-03-28 09:40:13 +00:00
import { bookingReferenceMiddleware } from "./middleware" ;
2022-04-14 21:25:24 +00:00
2022-06-03 19:21:46 +00:00
const prismaOptions : Prisma.PrismaClientOptions = { } ;
2023-10-24 19:59:15 +00:00
const globalForPrisma = global as unknown as {
prismaWithoutClientExtensions : PrismaClientWithoutExtension ;
prismaWithClientExtensions : PrismaClientWithExtensions ;
} ;
2022-06-03 19:21:46 +00:00
if ( ! ! process . env . NEXT_PUBLIC_DEBUG ) prismaOptions . log = [ "query" , "error" , "warn" ] ;
2023-10-24 19:59:15 +00:00
// Prevents flooding with idle connections
const prismaWithoutClientExtensions =
globalForPrisma . prismaWithoutClientExtensions || new PrismaClientWithoutExtension ( prismaOptions ) ;
2022-02-15 20:30:52 +00:00
2023-09-19 21:02:57 +00:00
export const customPrisma = ( options? : Prisma.PrismaClientOptions ) = >
new PrismaClientWithoutExtension ( { . . . prismaOptions , . . . options } ) . $extends ( withAccelerate ( ) ) ;
2023-08-24 09:14:10 +00:00
// If any changed on middleware server restart is required
// TODO: Migrate it to $extends
bookingReferenceMiddleware ( prismaWithoutClientExtensions ) ;
// FIXME: Due to some reason, there are types failing in certain places due to the $extends. Fix it and then enable it
// Specifically we get errors like `Type 'string | Date | null | undefined' is not assignable to type 'Exact<string | Date | null | undefined, string | Date | null | undefined>'`
2023-09-19 21:02:57 +00:00
const prismaWithClientExtensions = prismaWithoutClientExtensions
//
. $extends ( withAccelerate ( ) ) ;
// .$extends({
2023-08-24 09:14:10 +00:00
// query: {
// $allModels: {
// async $allOperations({ model, operation, args, query }) {
// const start = performance.now();
// /* your custom logic here */
// const res = await query(args);
// const end = performance.now();
// logger.debug("Query Perf: ", `${model}.${operation} took ${(end - start).toFixed(2)}ms\n`);
// return res;
// },
// },
// },
// });
// .$extends({
// name: "teamUpdateWithMetadata",
// query: {
// team: {
// async update({ model, operation, args, query }) {
// if (args.data.metadata) {
// // Prepare args.data with merged metadata
// }
// return query(args);
// },
// },
// },
// })
2023-10-24 19:59:15 +00:00
export const prisma = globalForPrisma . prismaWithClientExtensions || prismaWithClientExtensions ;
2022-06-14 17:11:39 +00:00
2022-02-15 20:30:52 +00:00
if ( process . env . NODE_ENV !== "production" ) {
2023-10-24 19:59:15 +00:00
globalForPrisma . prismaWithoutClientExtensions = prismaWithoutClientExtensions ;
globalForPrisma . prismaWithClientExtensions = prisma ;
2022-02-15 20:30:52 +00:00
}
2023-10-24 19:59:15 +00:00
type PrismaClientWithExtensions = typeof prismaWithClientExtensions ;
export type PrismaClient = PrismaClientWithExtensions ;
2022-02-15 20:30:52 +00:00
export default prisma ;
2022-05-18 21:05:49 +00:00
export * from "./selects" ;