GithubHelp home page GithubHelp logo

clayrisser / nestjs-crud-prisma Goto Github PK

View Code? Open in Web Editor NEW
60.0 2.0 13.0 1.06 MB

crud for restful apis built with nestjs and prisma

License: MIT License

Makefile 31.44% TypeScript 66.28% Dockerfile 1.39% Shell 0.89%

nestjs-crud-prisma's Introduction

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

nestjs-crud-prisma's People

Contributors

austinwebdeveloper avatar clayrisser avatar lavanya881 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nestjs-crud-prisma's Issues

Library does not follow prisma naming schema anymore

As per their docs, the new naming convention for table names use PascalCase. This library, on the other hand, uses camelCase as per:

this.tableName = camelcase(entity.name);
this.client = this.prisma[this.tableName];

This causes the whole library to break. Would it be possible to either use PascalCase instead or, at the very least, make the table name configurable? (maybe have a passed in options parameter or something?)

Enforce only filter/or/search

I think filter, or and search cannot be used with eachother in the same query. I just need to make sure this is correctly enforced.

`client.findOne` is not a function

I'm getting this error requesting a single entity, /users/1 for example.

[Nest] 47207   - 06/20/2021, 6:27:47 AM   [ExceptionsHandler] _this6.client.findOne is not a function +17556ms
TypeError: _this6.client.findOne is not a function
    at /Users/sebas/Sites/ponty.app/node_modules/nestjs-crud-prisma/src/prismaCrudService.ts:389:24
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/Users/sebas/Sites/ponty.app/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
    at _next (/Users/sebas/Sites/ponty.app/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
    at /Users/sebas/Sites/ponty.app/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
    at new Promise (<anonymous>)
    at /Users/sebas/Sites/ponty.app/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12
    at UsersService.getOne (/Users/sebas/Sites/ponty.app/node_modules/nestjs-crud-prisma/src/prismaCrudService.ts:387:46)
    at UsersController.getOneBase (/Users/sebas/Sites/ponty.app/node_modules/@nestjsx/crud/src/crud/crud-routes.factory.ts:211:27)
    at /Users/sebas/Sites/ponty.app/node_modules/@nestjs/core/router/router-execution-context.js:38:29

On the master branch the PrismaCrudService is using the findUnique method but in the npm package it's using findOne.

Add support for upsert?

Prisma supports upserting. I'm not sure which REST endpoint would be responsible for upserting but it could be super useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.