perf: Enable Prisma Optimize (#44)

* Enable Prisma Optimize

* Added Environment variable to toggle on and off the Prisma Optimize

---------

Co-authored-by: harshsbhat <icybhat@gmail.com>
This commit is contained in:
Harsh Shrikant Bhat
2024-08-01 02:05:14 +05:30
committed by GitHub
parent 75afd339ea
commit 0c072579b9
6 changed files with 311 additions and 15 deletions

View File

@@ -46,6 +46,10 @@ export const env = createEnv({
ADMIN_EMAIL: z.string().optional(),
DISCORD_WEBHOOK_URL: z.string().optional(),
REDIS_URL: z.string(),
ENABLE_PRISMA_CLIENT: z
.string()
.default("false")
.transform((str) => str === "true"), // Converts string "true" to boolean true
},
/**
@@ -84,6 +88,7 @@ export const env = createEnv({
ADMIN_EMAIL: process.env.ADMIN_EMAIL,
DISCORD_WEBHOOK_URL: process.env.DISCORD_WEBHOOK_URL,
REDIS_URL: process.env.REDIS_URL,
ENABLE_PRISMA_CLIENT: process.env.ENABLE_PRISMA_CLIENT, // Add this line
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially

View File

@@ -1,12 +1,18 @@
import { PrismaClient } from "@prisma/client";
import { withOptimize } from "@prisma/extension-optimize";
import { env } from "~/env";
const createPrismaClient = () =>
new PrismaClient({
const createPrismaClient = () => {
const client = new PrismaClient({
log:
env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
});
if (env.ENABLE_PRISMA_CLIENT) {
return client.$extends(withOptimize());
}
return client;
};
// eslint-disable-next-line no-undef
const globalForPrisma = globalThis as unknown as {