All files / src/modules/shared/database/prisma Prisma.service.ts

38.46% Statements 5/13
0% Branches 0/2
33.33% Functions 1/3
33.33% Lines 4/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30              4x 9x 9x     9x                                  
import { AsyncLocalStorage } from "node:async_hooks";
import { Inject, Injectable, Logger, OnApplicationShutdown, OnModuleInit } from "@nestjs/common";
import { PrismaClient } from "@prisma/client";
import { AlsStore } from "../als/types";
import { Repositories } from "../types/constants";
 
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnApplicationShutdown {
	public constructor(@Inject(Repositories.ALS) private readonly als: AsyncLocalStorage<AlsStore>) {
		super();
	}
 
	private readonly logger = new Logger(PrismaService.name);
 
	public async onModuleInit() {
		const state = this.als.getStore()["PrismaConnected"];
		if (!state) {
			await this.$connect();
			this.logger.log("PostgreSQL connected via Prisma");
			this.als.getStore()["PrismaConnected"] = true;
		}
	}
 
	public async onApplicationShutdown(signal?: string) {
		await this.$disconnect();
		this.logger.log(`PostgreSQL disconnected from Prisma reason: ${signal}`);
		this.als.getStore()["PrismaConnected"] = false;
	}
}