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

@vishivish18/nest-crudzilla

v1.0.4

Published

NestCRUDZilla is a powerful and intuitive package designed to streamline the implementation of CRUD (Create, Read, Update, Delete) functionality within NestJS applications. Say goodbye to repetitive code and tedious setup processes – NestCRUDMatic provide

Downloads

160

Readme

nest-crudzilla

Designer (2)

nest-crudzilla is a powerful NestJS package designed to simplify CRUD (Create, Read, Update, Delete) operations in your application. With nest-crudzilla, you can effortlessly build RESTful APIs for your entities while maintaining flexibility and customization options.

Features

  • CRUD Operations: Automate CRUD operations for your entities with minimal setup and configuration.
  • Flexible Configuration: Customize CRUD behavior to fit your application's specific needs, including support for custom DTOs, validation, and error handling.
  • Validation and Error Handling: Seamlessly integrate with NestJS validation and error handling mechanisms for robustness and reliability.
  • Pagination and Filtering: Support for pagination and filtering of query results to enhance performance and user experience.
  • ORM: Currently built for Mongoose ORM only.

Installation

Install @vishivish18/nest-crudzilla via npm or yarn:

npm install @vishivish18/nest-crudzilla
# or
yarn add @vishivish18/nest-crudzilla

Usage

Extend CrudController in Your Controller

import { Controller } from '@nestjs/common';
import { CrudController } from '@vishivish18/nest-crudzilla';
import { YourModel } from './your-model.model'; // Import your mongoose model

@Controller('your-resource')
export class YourController extends CrudController<YourModel> {
  constructor(private readonly yourService: YourService) {
    super(yourService);
  }
}

Extend CrudSerice in Your Service

import { CrudService } from '@vishivish18/nest-crudzilla';
import { YourModel } from './your-model.model'; // Import your mongoose model

export class YourService extends CrudService<YourModel> {
    constructor(
    @InjectModel('YourModel', ConnectionName.YourConnection)
    private readonly yourModel: Model<YourModel>,
  ) {
    super(yourModel);
  }
}

Example

Create your DTO (It is optional)

You can create both Create and Update DTO

import { IsBoolean, IsNumber, IsOptional, IsString } from "class-validator";
export class CreateCouponDto {
  @IsOptional()
  @IsString()
  code: string;

  @IsOptional()
  @IsString()
  discountType: string;

  @IsOptional()
  @IsNumber()
  discountValue: number;

  @IsOptional()
  @IsString()
  startDate: Date;

  @IsOptional()
  @IsString()
  endDate: Date;
}

Create your own usual NestJS controller and extend CrudController, pass Create and Update DTOs in constructor

import { Controller,Get,Post, Delete, Body, Patch, Param,} from '@nestjs/common';
import { CouponsService } from './coupons.service';
import { Coupon } from 'models/dist/models/coupons.schema';
import { CreateCouponDto } from './dto/create-coupon.dto';
import { UpdateCouponDto } from './dto/update-coupon.dto';
import { CrudController } from '@vishivish18/nest-crudzilla';

@Controller('coupons')
export class CouponsController extends CrudController<Coupon> {
  constructor(
    private readonly couponsService: CouponsService
    ) {
    super(couponsService, CreateCouponDto, UpdateCouponDto);
  }
    // These routes need not be explicitly created as they will be automatically handled by the base class CrudController:
    /**
     *  1.POST /api/coupons
     * 2. GET /api/coupons
     * 3. GET /api/coupons/:id
     * 4. PATCH /api/coupons/:id
     * 5. PUT /api/coupons/:id
     * 6. DELETE /api/coupons/:id 
    /*
 
  // Custom Controller endpoint to apply a coupon with the given code to the current order
  /**
   * Apply a coupon with the given code to the current order.
   *
   * @param couponCode The coupon code to apply.
   * @returns Details of the applied coupon, such as the discount type and value.
   * @throws Error if the provided coupon code is invalid, inactive, or expired.
   */
  @Get(':couponCode/apply')
  applyCoupon(
    @Param('couponCode') couponCode: string,
  ) {
    return this.couponsService.applyCoupon(couponCode);
  }

}

Create your own usual NestJS service and extend CrudService

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Coupon } from 'models/dist/models/coupons.schema';
import { ConnectionName } from 'src/utils/connectionName';
import { Model } from 'mongoose';
import { CouponDetailsDto } from './dto/coupon-details.dto';
import { CrudService} from '@vishivish18/nest-crudzilla';

@Injectable()
export class CouponsService extends CrudService {
  constructor(
    @InjectModel('Coupon', ConnectionName.ADMIN)
    private readonly couponModel: Model<Coupon>,
  ) {
    super(couponModel);
  }
 
  /**
  * The CRUD operations such as model.create(), model.find(), etc., need not be explicitly implemented in the service class,
  * as they will be automatically handled by the base class CrudService.
  **/


 /**
   * Public service function to Apply a coupon and calculate the discount.
   * @param couponCode The coupon code to apply.
   * @returns A CouponDetailsDto (Data Transfer Object) containing the details of the applied coupon.
   * @throws Error if the coupon is invalid, inactive, or expired.
   */
  public async applyCoupon(couponCode: string,):Promise<CouponDetailsDto>{
    const coupon = await this.couponModel.findOne({ code: couponCode });
    if (!coupon) {
      throw new Error('Invalid coupon code');
    }
    // check if coupon is active and not expired
    if (!coupon.isActive) {
      throw new Error('Coupon is not active');
    }
    if (coupon.startDate > new Date()) {
      throw new Error('Coupon is not active');
    }
    if (coupon.endDate && coupon.endDate < new Date()) {
      throw new Error('Coupon is expired');
    }
    return {
      discountType: coupon.discountType,
      discountValue: coupon.discountValue,
    };
  }
}