TypeORM(4)
-
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