Step-1: Create NestJS Project
Install the NestJS CLI and create a new project:
npm install -g @nestjs/cli
nest new nestjs-prisma
When prompted, select npm as your package manager. Navigate to the project directory:
cd nestjs-prisma
Step-2: Setup Prisma
Install Dependencies: Install the necessary Prisma packages and database drivers:
npm install prisma --save-dev
npm install @prisma/client @prisma/adapter-pg pg
Step-2.1: Initialize Prisma
Initialize Prisma in your project:
npx prisma init --output ../src/generated/prisma
It will also set the client output from "../generated/prisma” to “../src/generated/prisma”
Step-2.2: Set the generator output path
Note: Specify your output path for the generated Prisma client by either passing --output ../src/generated/prisma during prisma init
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
}
Step-2.3: Configure the module format #
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
moduleFormat = "cjs"
}
Note: The moduleFormat configuration is required because Prisma v7 ships as an ES module by default, which does not work with NestJS's CommonJS setup. Setting moduleFormat to cjs forces Prisma to generate a CommonJSmodule instead of ESM.
Step-3: Configure PostgreSQL Connection
Edit .env
DATABASE_URL="postgresql://postgres:password@localhost:5432/nestdb?schema=public"
Step-4: Define your data model
Add the following two models to your schema.prisma file:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean? @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
Step-5: Run Migration
npx prisma migrate dev --name init
Step-6: Generate Prisma Client
Once installed, you can run the generate command to generate the types and Client needed for your project.
npx prisma generate
Step-7: Create a Prisma service
nest g module prisma
nest g service prisma
Inside the src directory, create a new file called prisma.service.ts and add the following code to it:
import { Injectable } from "@nestjs/common";
import { PrismaClient } from "../generated/prisma/client.js";
import { PrismaPg } from "@prisma/adapter-pg";
@Injectable()
export class PrismaService extends PrismaClient {
constructor() {
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL as string,
});
super({ adapter });
}
}
Step-8: Register services in the app module (Optional)
Remember to register the new services in the app module.
Optional: If you create a module for prisma service. Otherwise paste below code.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma/prisma.service';
@Module({
imports: [PrismaModule],
controllers: [AppController],
providers: [AppService, PrismaService],
})
export class AppModule {}
Step-9: Test Setup
npm run start
or
npm run start:dev
Step-10: Create services
Now create services and use it like you do in normal Nest app.
----- 📄 Document End 🎉 -----
Top comments (0)