ghostfolio/apps/api/src/services/prisma/prisma.service.ts
Thomas Kaul db53ef54c4
Feature/use log levels to conditionally log prisma query events (#4003)
* Use LOG_LEVELS for prisma query events

* Update changelog
2024-10-30 21:39:51 +01:00

48 lines
1010 B
TypeScript

import {
Injectable,
Logger,
LogLevel,
OnModuleDestroy,
OnModuleInit
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Prisma, PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
public constructor(configService: ConfigService) {
let customLogLevels: LogLevel[];
try {
customLogLevels = JSON.parse(
configService.get<string>('LOG_LEVELS')
) as LogLevel[];
} catch {}
const log: Prisma.LogDefinition[] =
customLogLevels?.includes('debug') || customLogLevels?.includes('verbose')
? [{ emit: 'stdout', level: 'query' }]
: [];
super({
log,
errorFormat: 'colorless'
});
}
public async onModuleInit() {
try {
await this.$connect();
} catch (error) {
Logger.error(error, 'PrismaService');
}
}
public async onModuleDestroy() {
await this.$disconnect();
}
}