chore: add db debug support

This commit is contained in:
moonrailgun 2024-01-09 20:35:20 +08:00
parent 0076a3bed7
commit f9ea835dac
2 changed files with 29 additions and 2 deletions

View File

@ -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] + "'";
})
);
});
}

View File

@ -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 {