0
0
NestjsHow-ToBeginner ยท 4 min read

How to Use Prisma with NestJS: Setup and Example

To use Prisma with NestJS, install Prisma packages, generate the client, then create a PrismaService that extends PrismaClient. Inject this service into your modules to access the database with type-safe queries.
๐Ÿ“

Syntax

Here is the basic syntax pattern to integrate Prisma with NestJS:

  • Install Prisma: Use npm install @prisma/client and npm install prisma --save-dev.
  • Generate Prisma Client: Run npx prisma generate after defining your schema.
  • Create PrismaService: Extend PrismaClient and use @Injectable() to make it injectable.
  • Use PrismaService: Inject it into your modules and services to perform database operations.
typescript
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
  async onModuleInit() {
    await this.$connect();
  }

  async onModuleDestroy() {
    await this.$disconnect();
  }
}
๐Ÿ’ป

Example

This example shows a simple NestJS module using PrismaService to fetch all users from the database.

typescript
import { Module, Injectable } from '@nestjs/common';
import { User } from '@prisma/client';
import { PrismaService } from './prisma.service';

@Injectable()
export class UserService {
  constructor(private prisma: PrismaService) {}

  async getAllUsers(): Promise<User[]> {
    return this.prisma.user.findMany();
  }
}

@Module({
  providers: [PrismaService, UserService],
  exports: [UserService],
})
export class UserModule {}

// Usage in a controller or another service
// const users = await userService.getAllUsers();
Output
[{ id: 1, name: 'Alice', email: 'alice@example.com' }, { id: 2, name: 'Bob', email: 'bob@example.com' }]
โš ๏ธ

Common Pitfalls

Common mistakes when using Prisma with NestJS include:

  • Not calling $connect() and $disconnect() in PrismaService, which can cause connection leaks.
  • Injecting PrismaClient directly instead of using a service wrapper, leading to poor lifecycle management.
  • Forgetting to run npx prisma generate after schema changes, causing runtime errors.
typescript
/* Wrong way: Using PrismaClient directly without service */
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function getUsers() {
  return prisma.user.findMany();
}

/* Right way: Use PrismaService with lifecycle hooks */
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
  async onModuleInit() {
    await this.$connect();
  }

  async onModuleDestroy() {
    await this.$disconnect();
  }
}
๐Ÿ“Š

Quick Reference

StepCommand/CodeDescription
1npm install @prisma/client prisma --save-devInstall Prisma packages
2npx prisma initInitialize Prisma schema and config
3Define schema.prismaSet your database models
4npx prisma generateGenerate Prisma Client
5Create PrismaServiceWrap PrismaClient with NestJS service
6Inject PrismaServiceUse in modules and services for DB access
โœ…

Key Takeaways

Create a PrismaService extending PrismaClient with lifecycle hooks for connection management.
Always run 'npx prisma generate' after schema changes to update the client.
Inject PrismaService into your NestJS modules to access the database safely.
Avoid using PrismaClient directly to prevent connection and lifecycle issues.
Use async/await with Prisma queries inside your NestJS services for clean code.