◾️ this memo is from nomadcoders lecture
- install nestjs
$ npm i -g @nestjs/cli
$ nest new project-name2. install test cli tool(insomnia) for restapi / graphql
3. nest command for help
- nest generate controller – nest g c
- nest generate service – nest g s

4. Controller sample fro CRUD(post/get/patch or put/delete)
import {
  Controller,
  Post,
  Get,
  Patch,
  Delete,
  Param,
  Body,
  Query,
} from '@nestjs/common';
@Controller('movies')
export class MoviesController {
  @Post()
  create(@Body() movieData) {
    console.log(movieData);
    return { ...movieData };
  }
  @Get()
  getAll() {
    return 'This will return all movies';
  }
  @Get('search')
  search(@Query('year') searchYear: string) {
    return `We search for a movie ${searchYear}`;
  }
  @Get(':id')
  getOne(@Param('id') movieId: string) {
    return `This will return one movie ${movieId}`;
  }
  @Patch(':id')
  patch(@Param('id') movieId: string, @Body() updateData) {
    return {
      updateMovie: movieId,
      ...updateData,
    };
  }
  @Delete(':id')
  remove(@Param('id') movieId: string) {
    return `This will delete the ${movieId} : movie`;
  }
}
5. Service (business logic)
6. dto and input validator (ex- option field : readonly title?: string;)
import { IsString, IsNumber } from 'class-validator';
export class CreateMovieDto {
  @IsString()
  readonly title: string;
  @IsNumber()
  readonly year: number;
  @IsString({ each: true })
  readonly genres: string[];
}- install package for validator : npm i class-validator
- need class-validator annotation of dto when you use whitelist:true option
...
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
    }),
  );
  await app.listen(3000);
}
bootstrap();....
import { CreateMovieDto } from './dto/create-movie.dto';
@Controller('movies')
export class MoviesController {
  constructor(private readonly moviesService: MoviesService) {}
  @Post()
  create(@Body() movieData: CreateMovieDto) {
    return this.moviesService.create(movieData);
  }
....import { IsBoolean, IsString, Length } from 'class-validator';
....
{
  @Field((type) => String)
  @IsString()
  @Length(5, 10)
  name: string;
  @Field((type) => Boolean, { nullable: true })
  @IsBoolean()
  isVegan?: boolean;
}
....
7. type transform (ex : input string -> number in controller / service)
- install package for type transform : npm i class-transformer
....
import { ValidationPipe } from '@nestjs/common';
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
    }),
  );
  await app.listen(3000);
}
bootstrap();....
@Get(':id')
  getOne(@Param('id') movieId: number): Movie {
    return this.moviesService.getOne(movieId);
  }
....

8. create optional update-dto extends create-dto
- npm i @nestjs/mapped-types
import { PartialType } from '@nestjs/mapped-types';
import { CreateMovieDto } from './create-movie.dto';
export class UpdateMovieDto extends PartialType(CreateMovieDto) {}
9. usage playground of graphql : query and mutation



