Created
November 4, 2022 14:24
-
-
Save jacobhq/8c766ed0be5bda1c0ecc872bef027af1 to your computer and use it in GitHub Desktop.
Use prisma in nextjs with autocomplete and connection pooling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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