// src/server/db/client.ts
import { PrismaClient } from '@prisma/client'
import { logger } from '@/lib/logger'
const dbLogger = logger.child({ context: 'prisma' })
export const db = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'event',
level: 'error',
},
{
emit: 'event',
level: 'info',
},
{
emit: 'event',
level: 'warn',
},
],
})
db.$on('query', (e) => {
dbLogger.debug({
query: e.query,
params: e.params,
duration: e.duration,
}, 'Query executed')
})
db.$on('error', (e) => {
dbLogger.error({
target: e.target,
message: e.message,
}, 'Database error occurred')
})
db.$on('info', (e) => {
dbLogger.info({
message: e.message,
}, 'Database info')
})
db.$on('warn', (e) => {
dbLogger.warn({
message: e.message,
}, 'Database warning')
})