You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ghostfolio/apps/api/src/services/prisma/prisma.service.ts

54 lines
1.2 KiB

import {
Injectable,
Logger,
LogLevel,
OnModuleDestroy,
OnModuleInit
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PrismaPg } from '@prisma/adapter-pg';
import { Prisma, PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService
extends PrismaClient
implements OnModuleInit, OnModuleDestroy
{
public constructor(configService: ConfigService) {
const adapter = new PrismaPg({
connectionString: configService.get<string>('DATABASE_URL')
});
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({
adapter,
log,
errorFormat: 'colorless'
});
}
public async onModuleInit() {
try {
await this.$connect();
} catch (error) {
Logger.error(error, 'PrismaService');
}
}
public async onModuleDestroy() {
await this.$disconnect();
}
}