Performance: Prisma setup in NestJS
MEDIUM IMPACT
This affects server response time and API data fetching speed, impacting how fast the frontend receives data.
import { PrismaClient } from '@prisma/client'; import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; @Injectable() export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy { async onModuleInit() { await this.$connect(); } async onModuleDestroy() { await this.$disconnect(); } } @Injectable() export class UserService { constructor(private prisma: PrismaService) {} async getUsers() { return await this.prisma.user.findMany(); } }
import { Injectable } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; @Injectable() export class UserService { private prisma = new PrismaClient(); async getUsers() { return await this.prisma.user.findMany(); } }
| Pattern | DB Connections | Memory Usage | Response Latency | Verdict |
|---|---|---|---|---|
| Direct PrismaClient in service | Multiple per service instance | High | Higher due to connection overhead | [X] Bad |
| Singleton PrismaService with DI | Single shared connection | Low | Lower due to reuse | [OK] Good |