NestJS + TypeScript + Prisma
Build a structured, enterprise-grade REST API with NestJS's module system, dependency injection, Prisma ORM, and PostgreSQL.
Install the NestJS CLI
The NestJS CLI scaffolds the full project structure and generates modules, controllers, and services with a single command.
npm install -g @nestjs/cliCreate a New Project
Scaffold a new NestJS app. Choose npm when prompted (or your preferred package manager). The CLI sets up TypeScript, ESLint, Jest, and the full src/ structure automatically.
nest new my-nest-apiInstall Prisma and Dependencies
Install Prisma as a dev dep, the Prisma Client as runtime, and class-validator + class-transformer for DTO validation.
npm install @prisma/client class-validator class-transformer && npm install -D prismaInitialize Prisma
Generate the prisma/schema.prisma file and .env.
npx prisma init --datasource-provider postgresqlDefine the Schema
Add a User model to prisma/schema.prisma.
Run Migration
npx prisma migrate dev --name initCreate the PrismaService
NestJS uses dependency injection — wrap PrismaClient in an @Injectable() service so any module can import it. The onModuleInit hook opens the connection when the app starts.
Create the PrismaModule
Export PrismaService from its own module so any feature module can import it.
Generate the Users Resource
The CLI generates a complete users/ folder with module, controller, service, and DTOs.
nest generate resource users --no-specCreate the DTOs
DTOs define the shape of request bodies and are validated automatically by the ValidationPipe.
Implement the UsersService
Replace the generated stub with real Prisma queries.
Wire Up the Module and Enable Validation
Import PrismaModule in UsersModule, then enable ValidationPipe globally in main.ts so DTOs are validated on every request.
Start the Development Server
npm run start:devFound this guide helpful? Check out more tools and guides.
Browse All GuidesPrerequisites
- Node.js 20+
- PostgreSQL database