38 lines
944 B
TypeScript
38 lines
944 B
TypeScript
import {
|
|
Inject,
|
|
Injectable,
|
|
OnModuleDestroy,
|
|
OnModuleInit,
|
|
} from '@nestjs/common';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { PrismaClient } from '../generated/prisma/client';
|
|
import { ENVIRONMENT } from '../config/environment.module';
|
|
import type { Environment } from '../config/environment';
|
|
|
|
@Injectable()
|
|
export class DatabaseService
|
|
extends PrismaClient
|
|
implements OnModuleInit, OnModuleDestroy
|
|
{
|
|
constructor(@Inject(ENVIRONMENT) environment: Environment) {
|
|
super({
|
|
adapter: new PrismaPg({
|
|
connectionString: environment.DATABASE_URL,
|
|
connectionTimeoutMillis: 3000,
|
|
query_timeout: 3000,
|
|
max: environment.DATABASE_POOL_SIZE,
|
|
}),
|
|
});
|
|
}
|
|
|
|
async onModuleInit(): Promise<void> {
|
|
await this.$connect();
|
|
}
|
|
async onModuleDestroy(): Promise<void> {
|
|
await this.$disconnect();
|
|
}
|
|
async ping(): Promise<void> {
|
|
await this.$queryRaw`SELECT 1`;
|
|
}
|
|
}
|