Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(prisma): HMR support (#3547)

Hassieb Pakzade06272c4df3b
diff --git a/packages/shared/src/db.ts b/packages/shared/src/db.ts
index 02db91b893..241045ac96 100644
--- a/packages/shared/src/db.ts
+++ b/packages/shared/src/db.ts
@@ -73,14 +73,40 @@ export class KyselySingleton {
createQueryCompiler: () => new PostgresQueryCompiler(),
},
}),
- }),
+ })
);
return KyselySingleton.instance;
}
}
-export const prisma = PrismaClientSingleton.getInstance();
-export const kyselyPrisma = KyselySingleton.getInstance();
+declare const globalThis: {
+ prismaGlobal: PrismaClient | undefined;
+ kyselyPrismaGlobal: { $kysely: Kysely<DB> } | undefined;
+} & typeof global;
+
+if (process.env.NODE_ENV === "development") {
+ globalThis.prismaGlobal ??= new PrismaClient(); // regular instantiation
+ globalThis.kyselyPrismaGlobal ??= globalThis.prismaGlobal.$extends(
+ kyselyExtension({
+ kysely: (driver) =>
+ new Kysely<DB>({
+ dialect: {
+ // This is where the magic happens!
+ createDriver: () => driver,
+ // Don't forget to customize these to match your database!
+ createAdapter: () => new PostgresAdapter(),
+ createIntrospector: (db) => new PostgresIntrospector(db),
+ createQueryCompiler: () => new PostgresQueryCompiler(),
+ },
+ }),
+ })
+ );
+};
+
+export const prisma =
+ globalThis.prismaGlobal ?? PrismaClientSingleton.getInstance();
+export const kyselyPrisma =
+ globalThis.kyselyPrismaGlobal ?? KyselySingleton.getInstance();
export * from "@prisma/client";