Skip to content

Instantly share code, notes, and snippets.

@dmakwt
Forked from guiliredu/0_NestJS-Cheatsheet.md
Created July 7, 2023 17:50
Show Gist options
  • Save dmakwt/34ebc13f52066ac26439ebd9983d733a to your computer and use it in GitHub Desktop.
Save dmakwt/34ebc13f52066ac26439ebd9983d733a to your computer and use it in GitHub Desktop.
Nest.js Cheatsheet

Nest.js Cheatsheet

Nest CLI

  • npm i -g @nestjs/cli

Packages

  • yarn add class-validator class-transformer
  • yarn add @nestjs/mapped-types

Commands

  • npm run start:dev / yarn start:dev
  • nest generate controller / nest g co
  • nest generate service / nest g s
  • nest generate modue <name> / nest g mo
  • nest g class address/dto/create-address.dto --no-spec

Decorators

  • @Injectable(): Make a resource injectable via DI
  • @Controller('route'):
  • @Get(), @Post('user/:id/update')
  • @HttpCode(HttpStatus.GONE): Change http status
  • @Param() params: All url params, @Param('id'): Only id param
  • @Query() params: All url params, @Query('id'): Only id param
  • @Body() body: All body values, @Body('id'): Only id value
  • @Res() response: Response object from express
  • @IsNumber(), @IsString(): Validations

Imports

  • import { Body, Controller, Get, Param, Post } from '@nestjs/common';
  • import { IsNumber, IsString } from 'class-validator'; - Validation
import { IsNumber, IsString } from 'class-validator';
export class CreateAddressDto {
@IsNumber()
readonly cep: number;
@IsString()
readonly address: string;
@IsString()
readonly city: string;
@IsString()
readonly state: string;
}
import { PartialType } from "@nestjs/mapped-types";
import { CreateAddressDto } from "./create-address.dto";
export class UpdateAddressDto extends PartialType(CreateAddressDto) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment