Get Started with NestJS and Prisma
Tutorial Goal
This tutorial goes through the creation of a basic NestJS application using Prisma 2 (herein simply known as Prisma) as an ORM. This tutorial will use a REST API but it can easily be extended to use GraphQL instead.
This tutorial will not cover NestJS basics nor Prisma basics. It will simply cover how to connect the two technologies.
Video Tutorial
If you learn better via video, watch my YouTube tutorial:
GitHub Repository
Create a new NestJS Application
yarn global add @nestjs/cli
nest new nestjs-with-prisma
Add Prisma to NestJS
yarn add @prisma/client
yarn add @prisma/cli -D
yarn prisma init
prisma init
bootstraps a Prisma project within the current directory. Move /prisma
to /src
.
Add Movie model to /src/prisma/schema.prisma
:
model Movie {
id Int @default(autoincrement()) @id
director String
movieName String
yearReleased Int
}
Create a Prisma migration.
yarn prisma migrate save --schema src/prisma/schema.prisma --experimental
Execute the Prisma migration.
yarn prisma migrate up --schema src/prisma/schema.prisma --experimental
Generate the Prisma Client based on the Movie
model defined in src/prisma/schema.prisma
.
yarn prisma generate --schema src/prisma/schema.prisma
Create Prisma Module + Service
nest g module prisma
nest g service prisma
In /src/prisma/prisma.service.ts
:
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient {}
Then export the PrismaService
from PrismaModule
(/src/prisma/prisma.module.ts
):
import { Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
Movies Module + Controller
nest g module movies
nest g controller movies
Import the PrismaModule
into our MoviesModule
(in /src/movies/movies.module.ts
) so we can use PrismaService
:
import { Module } from '@nestjs/common';
import { MoviesController } from './movies.controller';
import { PrismaModule } from 'src/prisma/prisma.module';
@Module({
imports: [PrismaModule],
controllers: [MoviesController],
})
export class MoviesModule {}
Create a Movie data transfer object (src/movies/movies.dto.ts
) to represent the movie object:
export class MovieDto {
id: number;
director: string;
movieName: string;
yearReleased: number;
}
In src/movies/movies.controller.ts
:
import { Controller, Get, Post, Body } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { MovieDto } from './movie.dto';
@Controller('movies')
export class MoviesController {
constructor(private readonly prismaService: PrismaService) {}
@Post()
create(
@Body() { director, movieName, yearReleased }: MovieDto,
): Promise<MovieDto> {
return this.prismaService.movie.create({
data: { director, movieName, yearReleased },
});
}
@Get()
findAll(): Promise<MovieDto[]> {
return this.prismaService.movie.findMany();
}
}
Test Your App
Start your application with:
yarn start:dev
Make HTTP Request to POST http://localhost:3000/movies with a body of:
{
"director": "George Lucas",
"movieName": "Star Wars",
"yearReleased": 1977
}
Make HTTP request to GET http://localhost:3000/movies. Your response should be as follows:
[
{
"id": 1,
"director": "George Lucas",
"movieName": "Star Wars",
"yearReleased": 1978
}
]
Nice job!
Top comments (5)
Thank you
Thanks for the video.
Just one question:
why did you move prisma folder to src forlder ? (Move /prisma to /src ) ?
Good question.
I would rather have everything Prisma-related (the schema and the NestJS module) in one folder rather than separated into two folders.
There's also a bit of personal preference there with wanting to keep my root folder as clean as possible.
Simple and awesome man, thanks a lot!
Thank you, Bruno!