2023-09-02 12:13:50 +00:00
|
|
|
import { PrismaClient } from '@prisma/client';
|
2024-01-09 12:35:20 +00:00
|
|
|
import { logger } from '../utils/logger';
|
|
|
|
import { env } from '../utils/env';
|
2023-09-02 12:13:50 +00:00
|
|
|
|
2024-01-09 12:35:20 +00:00
|
|
|
const debugEvent = {
|
|
|
|
emit: 'event',
|
|
|
|
level: 'query',
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
const log = env.dbDebug ? [debugEvent] : [];
|
|
|
|
|
|
|
|
export const prisma = new PrismaClient({
|
|
|
|
log,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (env.dbDebug) {
|
|
|
|
prisma.$on('query', async (e) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
// console.log(`${e.query} ${e.params}`);
|
|
|
|
|
|
|
|
const params = JSON.parse(e.params);
|
|
|
|
logger.info(
|
|
|
|
e.query.replace(/\$(\d+)/g, (_, index) => {
|
|
|
|
return "'" + params[Number(index) - 1] + "'";
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|