import { Injectable, OnApplicationShutdown, OnModuleInit } from '@nestjs/common'; import { PrismaClient } from './generated/client'; import { PrismaPg } from '@prisma/adapter-pg'; /** Prisma client shared by the whole app (global PrismaModule), connected to `DATABASE_URL` through the pg driver adapter. */ @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit, OnApplicationShutdown { constructor() { const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL }); // 'minimal': by default, error messages print the query arguments (emails, names, addresses...), which end up in the logs super({ adapter, errorFormat: 'minimal' }); } async onModuleInit() { await this.$connect(); } /** * Last shutdown hook (after `onModuleDestroy` and `beforeApplicationShutdown`, once the HTTP server is closed): * a synchronization still running is waited for in `beforeApplicationShutdown` and keeps the database until then. */ async onApplicationShutdown() { await this.$disconnect(); } }