Skip to content

Instantly share code, notes, and snippets.

@guiliredu
Last active March 22, 2024 22:16
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save guiliredu/0aa9e4d338bbeeac369a597e87c9ba46 to your computer and use it in GitHub Desktop.
Save guiliredu/0aa9e4d338bbeeac369a597e87c9ba46 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