npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cjr-unb/super-crud

v1.3.5

Published

An abstraction of a CRUD using Prisma for NestJS

Downloads

73

Readme

pt-br

@cjr-unb/super-crud

A generic superclass for NestJS that contains the implementation of a CRUD using Prisma.

Installation

It is necessary to have Prisma installed and at least one migration executed. After that, install the package:

npm i @cjr-unb/super-crud

How to use

Generate the resources of your model through the nest-cli. Make sure the new module imports the PrismaModule, which should contain the PrismaServices.

First, it is necessary to configure the return options of the CRUD operations. To do this, in your Services file, import the Prisma namespace, which has a type of the format 'ModelName'. Delegate that receives RejectOptions. After that, create an auxiliary type with the name of your model:

import { Prisma } from '@prisma/client';
import { RejectOptions } from '@cjr-unb/super-crud';

type UserModel = Prisma.UserDelegate<RejectOptions>;

Then obtain the defaultOptions object and the getCrud function.

import { CrudOptions, RejectOptions } from '@cjr-unb/super-crud';
import { Prisma } from '@prisma/client';

type UserModel = Prisma.UserDelegate<RejectOptions>;
const {defaultOptions, getCrud} = new CrudOptions<UserModel>().setOptions({})

You can customize the options by changing the setOptions argument:

import { CrudOptions, RejectOptions } from '@cjr-unb/super-crud';
import { Prisma } from '@prisma/client';

type UserModel = Prisma.UserDelegate<RejectOptions>;
const {defaultOptions, getCrud} = new CrudOptions<UserModel>().setOptions({
  select: { email: true, name: true, password: false },
});

After that, create your injectable Service class that should extend the superclass as follows:

import { Injectable } from '@nestjs/common';
import { CrudOptions, RejectOptions } from '@cjr-unb/super-crud';
import { Prisma } from '@prisma/client';
import { PrismaService } from 'src/prisma/prisma.service';

type UserModel = Prisma.UserDelegate<RejectOptions>;
const {defaultOptions, getCrud} = new CrudOptions<UserModel>().setOptions({});

@Injectable()
export class UserServices extends getCrud<
    Prisma.UserGetPayload<typeof defaultOptions>
>() {}

The type parameter of the function should be the type present in the Prisma namespace that has the format 'ModelName'.GetPayload, which receives the type of the defaultOptions object

Finally, add the class constructor that receives an instance of PrismaService. In the super, pass as the first argument the prisma.'modelName' and the defaultOptions object as the second argument.

import { Injectable } from '@nestjs/common';
import { CrudOptions, RejectOptions } from '@cjr-unb/super-crud';
import { Prisma } from '@prisma/client';
import { PrismaService } from 'src/prisma/prisma.service';

type UserModel = Prisma.UserDelegate<RejectOptions>;
const {defaultOptions, getCrud} = new CrudOptions<UserModel>().setOptions({});

@Injectable()
export class UsersService extends getCrud<
  Prisma.UserGetPayload<typeof defaultOptions>
>() {
  constructor(protected readonly prisma: PrismaService) {
    super(prisma.user, defaultOptions);
  }
}

And that's it! Now your service has all CRUD operations.

You can use the defaultOptions and the super methods to create new methods or override existing ones.