nestjs-rapid v0.3.1
nestjs-rapid is a tiny library made to speed up your development using NestJS framework
Features
- Validate TypeOrm entities using
EntityExistPipe,EntityUniquePipeandRelationExistPipe
Getting Started
Prerequisites
NestJS and TypeOrm are required
Installing
$ npm install --save nestjs-rapidUsage
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') customerId: string): Promise<Customer> {
// customer with id equal to customerId exists
}Composite primary keys and relation columns are also supported
Note: You can apply
EntityExistPipeonparamorbodyroute 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() manufacturer: Partial<Manufacturer>):Promise<Manufacturer> {
// manufacturer has unique name
}Relation columns are also supported
Note: You can use
EntityUniquePipewith 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() product: Omit<Product, 'id'>): Promise<Product> {
// product manufacturer exists
}License
nestjs-rapid is MIT licensed
5 years ago