Prisma Client

Initialize prisma client using typescript.

aqrlnAugust 6, 2022

1 min read

Prisma is an ORM that helps app developers build faster and make fewer errors.

If you get this kind of error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Try using the code below:

// lib/prisma.ts
import { PrismaClient } from "@prisma/client";

let prisma: PrismaClient;

if (process.env.NODE_ENV === "production") {
	prisma = new PrismaClient();
} else {
	let globalWithPrisma = global as typeof globalThis & {
		prisma: PrismaClient;
	};
	if (!globalWithPrisma.prisma) {
		globalWithPrisma.prisma = new PrismaClient();
	}
	prisma = globalWithPrisma.prisma;
}

export default prisma;