Compare commits

...

1 Commits

Author SHA1 Message Date
Morgan Vernay 054eaf089b fix: improve prisma singleton 2023-11-14 19:52:21 +02:00

View File

@ -11,47 +11,53 @@ if (!!process.env.NEXT_PUBLIC_DEBUG) prismaOptions.log = ["query", "error", "war
export const customPrisma = (options?: Prisma.PrismaClientOptions) => export const customPrisma = (options?: Prisma.PrismaClientOptions) =>
new PrismaClientWithoutExtension({ ...prismaOptions, ...options }).$extends(withAccelerate()); new PrismaClientWithoutExtension({ ...prismaOptions, ...options }).$extends(withAccelerate());
export const prisma = (function () { const prismaClient = (function () {
// Prevents flooding with idle connections let instance: unknown;
const prismaWithoutClientExtensions = new PrismaClientWithoutExtension(prismaOptions); return () => {
// If any changed on middleware server restart is required if (instance) return instance;
// TODO: Migrate it to $extends // Prevents flooding with idle connections
bookingReferenceMiddleware(prismaWithoutClientExtensions); const prismaWithoutClientExtensions = new PrismaClientWithoutExtension(prismaOptions);
// 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 // 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>'` // Specifically we get errors like `Type 'string | Date | null | undefined' is not assignable to type 'Exact<string | Date | null | undefined, string | Date | null | undefined>'`
const prismaWithClientExtensions = prismaWithoutClientExtensions const prismaWithClientExtensions = prismaWithoutClientExtensions
// //
.$extends(withAccelerate()); .$extends(withAccelerate());
// .$extends({ // .$extends({
// query: { // query: {
// $allModels: { // $allModels: {
// async $allOperations({ model, operation, args, query }) { // async $allOperations({ model, operation, args, query }) {
// const start = performance.now(); // const start = performance.now();
// /* your custom logic here */ // /* your custom logic here */
// const res = await query(args); // const res = await query(args);
// const end = performance.now(); // const end = performance.now();
// logger.debug("Query Perf: ", `${model}.${operation} took ${(end - start).toFixed(2)}ms\n`); // logger.debug("Query Perf: ", `${model}.${operation} took ${(end - start).toFixed(2)}ms\n`);
// return res; // return res;
// }, // },
// }, // },
// }, // },
// }); // });
// .$extends({ // .$extends({
// name: "teamUpdateWithMetadata", // name: "teamUpdateWithMetadata",
// query: { // query: {
// team: { // team: {
// async update({ model, operation, args, query }) { // async update({ model, operation, args, query }) {
// if (args.data.metadata) { // if (args.data.metadata) {
// // Prepare args.data with merged metadata // // Prepare args.data with merged metadata
// } // }
// return query(args); // return query(args);
// }, // },
// }, // },
// }, // },
// }) // })
return prismaWithClientExtensions; instance = prismaWithClientExtensions;
return prismaWithClientExtensions;
};
})(); })();
export const prisma = prismaClient();
export type PrismaClient = typeof prisma; export type PrismaClient = typeof prisma;
export default prisma; export default prisma;