PrismaP
Prisma16mo ago
1 reply
berry73

Why does this function not work?

I'm trying to create a prisma file (/lib/db.ts) to initialize from anywhere in my code when I need it. Why does $disccount and $use not work and how do i get it to work if it is necessary.

Here is code:

import { PrismaClient, Prisma } from '@prisma/client'
import { handleConnectionError } from '@/middleware'

const prisma = global.prisma || new PrismaClient()

if (process.env.NODE_ENV === 'production') {
    global.prisma = prisma
}

const prismaWithEvents = prisma as Prisma.PrismaClient
prismaWithEvents.$on('beforeExit', async() => {
    await prisma.$disconnect()
})

prisma.$use(handleConnectionError)

export default prisma
prisma-function-not-work.png
Was this page helpful?