Home

Awesome

nestjs-crud-prisma

GitHub stars

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

Installation

npm install --save nestjs-crud-prisma

Dependencies

Usage

  1. Setup prisma to support generating typegraphql.

    This step is not required but recommended. Typegraphql will generate the models from the schema.prisma.

    schema.prisma

    generator typegraphql {
      provider = "../node_modules/typegraphql-prisma/generator.js"
      output   = "../src/generated/type-graphql"
    }
    
  2. 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.

    user.service.ts

    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
      }
    }
    
  3. Create a crud controller that injects the previous service.

    user.controller.ts

    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) {}
    }
    

Support

Submit an issue

Screenshots

Contribute a screenshot

Contributing

Review the guidelines for contributing

License

MIT License

Jam Risser © 2020

Changelog

Review the changelog

Credits

Support on Liberapay

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!

Liberapay receiving Liberapay patrons