crud for restful apis built with nestjs and prisma
Please ★ this repo if you found it useful ★ ★ ★
Though not required, nestjs-crud-prisma works best with typegraphql-prisma
npm install --save nestjs-crud-prisma
-
Setup prisma to support generating typegraphql.
This step is not required but recommended. Typegraphql will generate the models from the schema.prisma.
generator typegraphql { provider = "../node_modules/typegraphql-prisma/generator.js" output = "../src/generated/type-graphql" }
-
Create a service that injects the prisma service.
Although it's not required, I recommend nestjs-prisma to get the prisma service. Also, notice I'm getting the model from the generated typegraphql.
import { Injectable } from '@nestjs/common'; import { PrismaCrudService } from 'nestjs-crud-prisma'; import { PrismaService } from 'nestjs-prisma'; import { User } from '../../generated/type-graphql'; @Injectable() export class UserService extends PrismaCrudService<User> { constructor(prisma: PrismaService) { super(prisma, User); // make sure you pass in the model } }
-
Create a crud controller that injects the previous service.
import { Controller } from '@nestjs/common'; import { Crud } from '@nestjsx/crud'; import { UserService } from './user.service'; import { User } from '../../generated/type-graphql'; @Crud({ model: { type: User }, params: { id: { field: 'id', type: 'string', primary: true } }, query: { alwaysPaginate: true } }) @Controller('users') export class UserController { constructor(public service: UserService) {} }
Submit an issue
Contribute a screenshot
Review the guidelines for contributing
Jam Risser © 2020
Review the changelog
- Jam Risser - Author
A ridiculous amount of coffee ☕ ☕ ☕ was consumed in the process of building this project.
Add some fuel if you'd like to keep me going!