Getting Started
info
This guide includes setup of NestJS and Prisma. See Quickstart if you already have those.
#
1. Setup NestJSInstall NestJS and navigate to project folder:
npm i -g @nestjs/clinest new my-projectcd my-project
#
2. Setup PrismaInstall prisma:
npm install prisma --save-devnpx prisma init
Add the following example into your
schema.prisma
file:schema.prismadatasource db { provider = "sqlite" url = "file:./dev.db"} generator client { provider = "prisma-client-js"} model Post { id String @id content String}
Push your changes to the database:
npx prisma generatenpx prisma migrate dev
Create the prisma service:
my-project/src/prisma.service.tsimport { 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(); }}
nestjs-prisma-crud
#
3. Setup Install
nestjs-prisma-crud
andnestjs-prisma-crud-schematics
:npm i nestjs-prisma-crud --savenpm i nestjs-prisma-crud-schematics --save-dev
Register the module:
app.module.tsimport { PrismaCrudModule } from 'nestjs-prisma-crud'; @Module({ imports: [ /** * PrismaCrudModule registers the PrismaService provider globally. * No need to provide it anywhere else! */ PrismaCrudModule.register({ prismaService: PrismaService, }), ], // ...})export class AppModule {}
Generate the entire crud module with the following command (replace post with your table's name from your Prisma schema):
nest g -c nestjs-prisma-crud-schematics crud-resource post
Start the server and verify installation:
npm run start:devcurl http://localhost:3000/post # 200 response json object
#
Whats next?- Learn more about the crud endpoints.
- Browse the access control documentation.