aws 강의 수강, sql 2회차

최수민·2023년 8월 4일
0

TIL

목록 보기
6/41

오전시간에는 국취제 면담을 다녀왔고, aws 강의를 수강하고 있습니다.
그리고 sql 2회차가 나왔다고 하여서 강의영상은 아직 나오지 않았기 때문에 문제를 먼저 풀어보려고 합니다.

면담을 다녀온 후 node.js를 하는데 정작 어느 부분에서 취업을 해야할지 고민이 많아 찾아보니 node.js를 사용하는데 같이 쓰이는 것이 typescript, express, nest.js 가 나와서 nest.js를 들으려고 합니다. 다듣기에는 무리지만 필요할거 같아 다 듣는것이 목표입니다.

nestjs 설치
<gitbash로 작성함>

  • 파일 생성
$ mkdir nestjs_practice
  • nest 설치
$ npm i -g @nestjs/cli
  • nest 확인
$ nest
  • 확인시 아래와 같이 나오면 됨
Usage: nest <command> [options]

Options:
  -v, --version                                   Output the current version.
  -h, --help                                      Output usage information.

Commands:
  new|n [options] [name]                          Generate Nest application.
  build [options] [app]                           Build Nest application.
  start [options] [app]                           Run Nest application.
  info|i                                          Display Nest project details.
  add [options] <library>                         Adds support for an external library to your project.
  generate|g [options] <schematic> [name] [path]  Generate a Nest element.
    Schematics available on @nestjs/schematics collection:
      ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
      │ name          │ alias       │ description                                  │
      │ application   │ application │ Generate a new application workspace         │
      │ class         │ cl          │ Generate a new class                         │
      │ configuration │ config      │ Generate a CLI configuration file            │
      │ controller    │ co          │ Generate a controller declaration            │
      │ decorator     │ d           │ Generate a custom decorator                  │
      │ filter        │ f           │ Generate a filter declaration                │
      │ gateway       │ ga          │ Generate a gateway declaration               │
      │ guard         │ gu          │ Generate a guard declaration                 │
      │ interceptor   │ itc         │ Generate an interceptor declaration          │
      │ interface     │ itf         │ Generate an interface                        │
      │ library       │ lib         │ Generate a new library within a monorepo     │
      │ middleware    │ mi          │ Generate a middleware declaration            │
      │ module        │ mo          │ Generate a module declaration                │
      │ pipe          │ pi          │ Generate a pipe declaration                  │
      │ provider      │ pr          │ Generate a provider declaration              │
      │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
      │ resource      │ res         │ Generate a new CRUD resource                 │
      │ service       │ s           │ Generate a service declaration               │
      │ sub-app       │ app         │ Generate a new application within a monorepo │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘
  • nest.js 프로젝트 생성 명령어
$ nest new sparta-nest
  • 위 생성 명령어를 작성하고 나면 아래와 같이 나옴 npm을 고르고 enter
? Which package manager would you ❤️  to use? (Use arrow keys)npm
  yarn
  pnpm
  • 그러고나면 아래와 같이 나오면 성공
? Which package manager would you ❤️  to use? (Use arrow keys)
? Which package manager would you ❤️  to use? npm
CREATE sparta-nest/.eslintrc.js (663 bytes)
CREATE sparta-nest/.prettierrc (51 bytes)
CREATE sparta-nest/nest-cli.json (171 bytes)
CREATE sparta-nest/package.json (1956 bytes)
CREATE sparta-nest/README.md (3340 bytes)
CREATE sparta-nest/tsconfig.build.json (97 bytes)
CREATE sparta-nest/tsconfig.json (546 bytes)
CREATE sparta-nest/src/app.controller.spec.ts (617 bytes)
CREATE sparta-nest/src/app.controller.ts (274 bytes)
CREATE sparta-nest/src/app.module.ts (249 bytes)
CREATE sparta-nest/src/app.service.ts (142 bytes)
CREATE sparta-nest/src/main.ts (208 bytes)
CREATE sparta-nest/test/app.e2e-spec.ts (630 bytes)
CREATE sparta-nest/test/jest-e2e.json (183 bytes)

- Installation in progress... ☕
√ Installation in progress... ☕

🚀  Successfully created project sparta-nest
👉  Get started with the following commands:

$ cd sparta-nest
$ npm run start


                          Thanks for installing Nest 🙏
                 Please consider donating to our open collective
                        to help us maintain this package.


                   🍷  Donate: https://opencollective.com/nest
  • 만약 실패시 아래 명령어 하나씩 실행
  • 성공했다면 실행하지 마세요!!
$ git clone https://github.com/nestjs/typescript-starter.git sparta-nest
$ cd sparta-nest
$ npm i

-- aws RDS 일시중지 함 --


IoC

  • Inversion of Control 준말

  • 제어역전 이라고도 함

  • 기존에 서비스를 사용하고 싶을땐 아래와 같이 코드 작성

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  // 1. 사용하고 싶은 서비스 타입 객체를 미리 선언합니다.
  private appService: AppService
  
  constructor() {
    // 2. 생성자에서 실제로 사용할 서비스 객체를 직접 생성합니다.
    this.appService = new AppService();
  }
  ...
}
  • 이렇게 직접 생성하면 의존하는 서비스가 변경되면 개발자도 그에 맞추어서 코드를 수정해야 합니다.

하지만, IoC는 개발자가 사용하고 싶은 객체를 직접 생성하는 것이 아니라 객체의 생명주기 관리 자체를 외부(여기서는 Nest.js IoC 컨테이너)에 위임을 합니다!

IoC는 모듈 간 결합도를 낮추기 때문에 하나의 모듈이 변경되어도
다른 모듈들에는 영향을 최소화
되어 웹 어플리케이션을 지속 가능하고
확장성 있게 해줍니다

  • 라이브러리프레임워크 차이
  • 라이브러리
    • 제어권이 나에게 있다.
    • 내 코드가 필요할 때마다 내가 사용하고 싶은 라이브러리를 사용한다.
  • 프레임워크
    • 제어권이 프레임워크에 있다.
    • 나의 코드를 프레임워크가 필요로 하는 경우에 프레임워크가 알아서 실행시킨다.

제어권을 넘겨주면 개발자는 비지니스 로직을 작성하는 데 더 집중을 할 수 있습니다. 비지니스 로직을 작성하기 위해 세팅해야 하는 사전작업이 최소화되는 것이에요.

DI

  • IoC를 수행하는 방법의 하나
  • 생성자를 통한 DI를 가장 기본적인 IoC 테크닉
constructor(private readonly appService: AppService) {} // 얼마나 편하게요~

추가 알아볼 자료

  • SOLID 원칙의 DIP(의존 역전 원칙)

1개의 댓글

comment-user-thumbnail
2023년 8월 4일

글 재미있게 봤습니다.

답글 달기