CRUD utility for simple REST use cases. Builds on top of NestJS and Prisma. Inspired by @nestjsx/crud.
npm i nestjs-prisma-crud --save
npm i nestjs-prisma-crud-schematics --save-dev
When building REST API's there is common functionality that we would prefer not to implement again and again. This package offers minimal and opinionated out of the box solutions for some of those (see features below).
An overview of the provided functionality:
- Advanced client side joining, sorting, filtering and pagination via query parameters
- Any valid prisma
.where
can be sent by the frontend. - Server side validation to safeguard against arbitrarily deep
.join
or.where
clauses by clients. - Support for including only specific properties in the response.
- Any valid prisma
- Access control
- Atomic operations
- Supports POST/PATCH with nested objects.
- Transaction support when extending controller functionality.
- Schematics
crud-resource
: a modified NestJSresource
schematic that scaffolds the entire CRUD module for you.
One-line scaffolding with:nest g -c nestjs-prisma-crud-schematics crud-resource <YOUR-TABLE-NAME-HERE>
- Plug and play
- Can be used alongside your other non
nestjs-prisma-crud
controllers. - You can still use
PrismaCrudService
and@AccessPolicy
in your custom controllers if you want to retain some ofnestjs-prisma-crud
's functionalities.
- Can be used alongside your other non
Assumes both NestJS and Prisma2 are already setup. See Getting Started.
-
Install
nestjs-prisma-crud
andnestjs-prisma-crud-schematics
:npm i nestjs-prisma-crud npm i nestjs-prisma-crud-schematics --save-dev
-
Generate the entire crud module with a single command (replace post with your model's name):
nest g -c nestjs-prisma-crud-schematics crud-resource post
-
Setup your service and controller
// post.service.ts import { Injectable } from '@nestjs/common'; import { PrismaCrudService } from 'nestjs-prisma-crud'; @Injectable() export class PostService extends PrismaCrudService { constructor() { super({ model: 'post', allowedJoins: ['comments.author'], }); } } // post.controller.ts import { PostService } from './post.service'; @Controller('post') export class PostController { constructor(private readonly postService: PostService) {} @Get() async findMany(@Query('crudQuery') crudQuery: string) { const matches = await this.postService.findMany(crudQuery); return matches; } }
Go here for full documentation.