Wondering what’s next for npm?Check out our public roadmap! »

    nestjs-rapid
    TypeScript icon, indicating that this package has built-in type declarations

    0.3.1 • Public • Published

    nestjs-rapid is a tiny library made to speed up your development using NestJS framework

    Features

    • Validate TypeOrm entities using EntityExistPipe, EntityUniquePipe and RelationExistPipe

    Getting Started

    Prerequisites

    NestJS and TypeOrm are required

    Installing

    $ npm install --save nestjs-rapid

    Usage

    EntityExistPipe ensures entity record exists, otherwise throws BadRequestException

    Consider Customer entity with a single primary column

    @Entity()
    export class Customer {
      @PrimaryGeneratedColumn('uuid')
      id: string;
     
      @Column()
      name: string;
    }

    Apply EntityExistPipe on the route parameter to make sure customer record exists

    import { EntityExistPipe } from 'nestjs-rapid';
     
    // route parameter path name must repeat entity primary property name. In this case id
    @UsePipes(new EntityExistPipe(Customer))
    @Get(':id')
    findOne(@Param('id') customerIdstring)Promise<Customer> {
      // customer with id equal to customerId exists
    }

    Composite primary keys and relation columns are also supported

    Note: You can apply EntityExistPipe on param or body route parameters

    EntityUniquePipe ensures entity record is unique, otherwise throws BadRequestException

    Suppose we have Manufacturer entity

    @Entity()
    @Unique(['name'])
    export class Manufacturer {
      @PrimaryGeneratedColumn('uuid')
      id: string;
     
      @Column()
      name: string;
    }

    Then you can check if Manufacturer is unique

    import { EntityUniquePipe } from 'nestjs-rapid';
     
    @UsePipes(new EntityUniquePipe(Manufacturer))
    @Post()
    create(@Body() manufacturerPartial<Manufacturer>):Promise<Manufacturer> {
      // manufacturer has unique name
    }

    Relation columns are also supported

    Note: You can use EntityUniquePipe with POST requests only

    RelationExistPipe ensures entity relation exists, otherwise throws BadRequestException

    Lets say we have Product entity with relation property manufacturer

    @Entity()
    export class Product {
      @PrimaryGeneratedColumn('uuid')
      id: string;
     
      @Column()
      name: string;
     
      @ManyToOne(() => Manufacturer, (manufacturer: Manufacturer) => manufacturer.products)
      manufacturer: Manufacturer;
    }

    To confirm that manufacturer exists write

    import { RelationExistPipe } from 'nestjs-rapid';
     
    // pass in relation type and lambda that returns relation property
    @UsePipes(new RelationExistPipe(Manufacturer, (product: Product) => product.manufacturer))
    @Post()
    create(@Body() productOmit<Product, 'id'>)Promise<Product> {
      // product manufacturer exists
    }

    License

    nestjs-rapid is MIT licensed

    Install

    npm i nestjs-rapid

    DownloadsWeekly Downloads

    2

    Version

    0.3.1

    License

    MIT

    Unpacked Size

    12.8 kB

    Total Files

    13

    Last publish

    Collaborators

    • avatar