Skip to content

Instantly share code, notes, and snippets.

@jacobhq
Created November 4, 2022 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobhq/8c766ed0be5bda1c0ecc872bef027af1 to your computer and use it in GitHub Desktop.
Save jacobhq/8c766ed0be5bda1c0ecc872bef027af1 to your computer and use it in GitHub Desktop.
Use prisma in nextjs with autocomplete and connection pooling.
import { PrismaClient } from "@prisma/client";
// PrismaClient is attached to the `global` object in development to prevent
// exhausting your database connection limit.
//
// Learn more:
// https://pris.ly/d/help/next-js-best-practices
const prismaClientPropertyName = `__prevent-name-collision__prisma`
type GlobalThisWithPrismaClient = typeof globalThis & {
[prismaClientPropertyName]: PrismaClient
}
const getPrismaClient = () => {
if (process.env.NODE_ENV === `production`) {
return new PrismaClient()
} else {
const newGlobalThis = globalThis as GlobalThisWithPrismaClient
if (!newGlobalThis[prismaClientPropertyName]) {
newGlobalThis[prismaClientPropertyName] = new PrismaClient()
}
return newGlobalThis[prismaClientPropertyName]
}
}
const prisma = getPrismaClient()
export default prisma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment