Stanislas de Roquemaurel Galitzine
07/29/2022, 3:14 PMimport { PrismaClient } from '@prisma/client';
export let prisma: PrismaClient;
if (typeof window === 'undefined') {
if (process.env['NODE_ENV'] === 'production') {
prisma = new PrismaClient();
} else {
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
if (!global.prisma) {
// @ts-ignore
global.prisma = new PrismaClient();
}
// @ts-ignore
prisma = global.prisma;
}
}
This works okay bar from the ugly ts-ignores, which I would like to remove.
I can fix this by prepending the code above with:
declare global {
var prisma: PrismaClient
}
But then it clashes with Jest which throws a Duplicate declaration "prisma" error.
What would be the correct way of fixing this, without exporting prisma as default ?
Why is Jest confused by this? (I am not sure about the error, seems like Jest is unable to infer that this is the same variable).Lloyd Richards
07/29/2022, 3:36 PMimport { PrismaClient } from "@prisma/client";
// Prevent multiple instances of Prisma Client in development
declare const global: Global & { prisma?: PrismaClient };
export const prisma =
global.prisma ||
new PrismaClient({
log:
process.env.NODE_ENV === "production"
? ["error"]
: ["error", "info", "warn"],
});
if (process.env.NODE_ENV === "development") global.prisma = prisma;Stanislas de Roquemaurel Galitzine
07/29/2022, 3:48 PMNathan Froese
08/04/2022, 3:35 PMdeclare const global: Global & { prisma: PrismaClient };Lloyd Richards
08/04/2022, 3:42 PM