Prisma helps you talk to your database easily. Setting it up in NestJS lets your app save and get data simply and safely.
0
0
Prisma setup in NestJS
Introduction
You want to connect your NestJS app to a database like PostgreSQL or MySQL.
You need a clear way to write database queries without raw SQL.
You want to keep your database code organized and easy to maintain.
You plan to use TypeScript and want type safety with your database.
You want to quickly generate and update your database schema.
Syntax
NestJS
1. Install Prisma and its dependencies: npm install @prisma/client npm install -D prisma 2. Initialize Prisma: npx prisma init 3. Define your database schema in prisma/schema.prisma 4. Generate Prisma Client: npx prisma generate 5. Create a PrismaService in NestJS to use Prisma Client: 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(); } } 6. Provide PrismaService in your module and inject it where needed.
Prisma Client is auto-generated code to talk to your database.
PrismaService manages database connection lifecycle in NestJS.
Examples
This defines a User table with id, name, and unique email.
NestJS
prisma/schema.prisma
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}Basic PrismaService without lifecycle hooks (simpler but less safe).
NestJS
import { Injectable } from '@nestjs/common'; import { PrismaClient } from '@prisma/client'; @Injectable() export class PrismaService extends PrismaClient { constructor() { super(); } }
Module to provide PrismaService for injection in other parts of the app.
NestJS
import { Module } from '@nestjs/common'; import { PrismaService } from './prisma.service'; @Module({ providers: [PrismaService], exports: [PrismaService], }) export class PrismaModule {}
Sample Program
This example shows how to set up PrismaService with lifecycle hooks, create a UserService to add and get users, and provide them in a NestJS module.
NestJS
import { Module, 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(); } } @Injectable() export class UserService { constructor(private prisma: PrismaService) {} async createUser(name: string, email: string) { return this.prisma.user.create({ data: { name, email } }); } async getUsers() { return this.prisma.user.findMany(); } } @Module({ providers: [PrismaService, UserService], exports: [UserService], }) export class AppModule {}
OutputSuccess
Important Notes
Always run npx prisma generate after changing your schema.
Use lifecycle hooks in PrismaService to manage database connections cleanly.
Inject PrismaService in your services to access the database safely.
Summary
Prisma setup in NestJS connects your app to a database with easy and safe code.
Define your database schema, generate Prisma Client, and create a PrismaService.
Use PrismaService in your NestJS services to read and write data.