diff --git a/src/server/model/_client.ts b/src/server/model/_client.ts index 9b6c4ce..fafa65d 100644 --- a/src/server/model/_client.ts +++ b/src/server/model/_client.ts @@ -1,3 +1,29 @@ import { PrismaClient } from '@prisma/client'; +import { logger } from '../utils/logger'; +import { env } from '../utils/env'; -export const prisma = new PrismaClient(); +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] + "'"; + }) + ); + }); +} diff --git a/src/server/utils/env.ts b/src/server/utils/env.ts index 6e21a88..4e0007e 100644 --- a/src/server/utils/env.ts +++ b/src/server/utils/env.ts @@ -4,7 +4,8 @@ export const env = { websiteId: process.env.WEBSITE_ID, sandboxMemoryLimit: process.env.SANDBOX_MEMORY_LIMIT ? Number(process.env.SANDBOX_MEMORY_LIMIT) - : 16, // MB + : 16, // unit: MB + dbDebug: checkEnvTrusty(process.env.DB_DEBUG), }; export function checkEnvTrusty(env: string | undefined): boolean {