nestJS(10)
-
Type ORM 알아보기 [NestJS]
TypeORM은 TS와 JS에서 사용할 수 있는 객체 관계 매핑(ORM) 라이브러리 중 하나이다. ORM은 데이터베이스와의 상호작용을 추상화하여 개발자가 객체 지향 프로그래밍 스타일을 사용하여 데이터베이스와 상호 작용할 수 있게 해준다. Column Annotation PrimaryGeneratedColumn 해당 필드를 기본 키로 사용하며, 자동으로 생성되는 숫자 형태의 값이다. 주로 순차적인 정수 값을 기본키로 사용할 때 활용된다. // @PrimaryGeneratedColumn() 은 자동으로 id 를 생성한다. // 1, 2, 3, 4, 5 -> 순서대로 id 값이 올라간다. // @PrimaryGeneratedColumn('uuid') // asd1rf123f-213dsaf3-123asdbe-o..
2023.12.15 -
TypeORM 함수 with NestJS
@Injectable() export class PostsService { constructor( @InjectRepository(PostsModel) private readonly postsRepository: Repository, ) {} //... } find 다수의 데이터 가져오기 async getAllPosts() { return this.postsRepository.find(); } findOne 하나의 데이터 찾기 async getPostById(id: number) { const post = await this.postsRepository.findOne({ where: { id, }, }); if (!post) { throw new NotFoundException(); } return po..
2023.12.14 -
NestJS TypeORM 테이블 생성 with PostgreSQL
선행작업 https://muyeon95.tistory.com/311 TypeORM을 사용해 클래스기반 자동 테이블 생성을 진행해본다. 이는 TypeORM 의 기능이다. // src/posts/entities/posts.entity.ts import { Column, Entity } from 'typeorm'; @Entity() export class PostsModel { @Column() id: number; @Column() author: string; @Column() title: string; @Column() content: string; @Column() likeCount: number; @Column() commentCount: number; } 엔티티 추가 TypeOrmModule.forR..
2023.12.14 -
NestJS TypeORM 설정 및 연결 with PostgreSQL
설치 npm install @nestjs/typeorm typeorm pg typeORM 세팅 app.module.ts 에 세팅한다. import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { PostsModule } from './posts/posts.module'; import { TypeOrmModule } from '@nestjs/typeorm'; @Module({ imports: [ PostsModule, TypeOrmModule.forRoot({ type: 'postgres', // 데이터베이스 타..
2023.12.14 -
Dependency Injection / Inversion of Control [의존성 주입 / 제어의 역전] [NestJS]
인스턴스를 생성하지 않았는데 컨트롤러에서 어떻게 주입을 받고 어떻게 서비스에 대한 권한과 접근이 가능할까? 이는 NestJS의 가장 핵심이 되는 요소중 하나이다. 일반 인스턴스화 클래스 B 를 클래스 A에 사용해야 한다면? class A { const b = B(); } class B { } A라는 클래스를 인스턴스로 만들때마다 A 안에 클래스 B의 인스턴스를 매번 새로 생성하게 된다. DI (Dependency Injection) class A { constructor(instance: B) } class B { } B라는 클래스를 생성을 해서 constructor에 입력해준다. 이를 주입이라고 한다. 외부에서 클래스 A가 생성될 때 무조건 클래스 B에 해당되는 인스턴스를 넣어주도록 정의를 하는 것이다..
2023.12.13 -
NestJS 요청 라이프 사이클 (NestJS Request Lifecycle)
요청 라이프 사이클이란? 요청이 디바이스로부터 서버로 보내진 다음에 응답이 되어서 돌아오는 과정을 전부 표현한 것 NestJS Docs 들어오는 요청 미들웨어 - 전역적으로 바인딩 된 미들웨어 - 모듈 바인딩 미들웨어 가드 - 글로벌 가드 - 컨트롤러 가드 - 라우트 가드 컨트롤러 이전 인터셉터 - 글로벌 인터셉터(컨트롤러 이전) - 컨트롤러 인터셉터(컨트롤러 이전) - 라우트 인터셉터(컨트롤러 이전) 파이프 - 글로벌 파이프 - 컨트롤러 파이프 - 라우트 파이프 - 매개변수 파이프 경로 지정 컨트롤러(메서드 핸들러) 서비스(존재하는 경우) 요청 후 인터셉터 - 라우트 인터셉터(요청 후) - 컨트롤러 인터셉터(요청 후) - 전역 인터셉터(요청 후) 예외 필터(라우트, 컨트롤러, 전역) 서버응답 아래 사진..
2023.12.11