본문 바로가기

프로그래밍/TypeORM18

[TypeORM] Caching queries https://orkhan.gitbook.io/typeorm/docs/caching QueryBuilder메소드(getMany, getOne, getRawMany, getRawOne, getCount)의 메소드 결과를 캐시할 수 있다. EntityManager나 Repository의 find*, count* 메소드의 결과 또한 캐시할 수 있다. data source의 옵션에서 명시적으로 캐싱을 활성화한다. { type: "mysql", host: "localhost", username: "test", ... cache: true } 한번 캐시를 활성화하면 데이터베이스 스키마를 다시 동기화해야 한다. CL를 사용하여 migration 또는 synchronize 데이터 소스 옵션을 처리한다. 이후에 Quer.. 2023. 8. 31.
[TypeORM] Active Record vs Data Mapper https://orkhan.gitbook.io/typeorm/docs/active-record-data-mapper What is the Active Record pattern? 액티브 레코드 패턴은 모델 내에 모든 쿼리를 작성한다. 그리고 이를 이용하여 모델을 저장하고 삭제하는 처리를 진행한다. 단순히 말해 모델 내에서 데이터베이스에 접근하는 것이다. import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from "typeorm" @Entity() export class User extends BaseEntity { @PrimaryGeneratedColumn() id: number @Column() firstName: string @Column(.. 2023. 8. 31.
[TypeORM] Entity Inheritance https://orkhan.gitbook.io/typeorm/docs/entity-inheritance 엔티티 상속 방식 1. 구체적인 테이블 상속 2. 싱글 테이블 상속 3. 임베디드 사용 Concrete Table Inheritance 엔티티 상속 패턴을 통해 코드를 줄일 수 있다. 가장 쉽고 효과적인 방법은 구체적 테이블 상속이다. 예를 들어 Photo, Question, Post 엔티티가 있다고 해보자. @Entity() export class Photo { @PrimaryGeneratedColumn() id: number @Column() title: string @Column() description: string @Column() size: string } @Entity() export cl.. 2023. 8. 31.
[TypeORM] Embedded Entities 임베디드 컬럼을 사용하여 중복을 줄일 수 있다. 자신의 컬럼을 가진 클래스를 받아들이는 컬럼이다. 그리고 이러한 컬럼을 현재 엔티티 테이블에 합친다. User, Employee 그리고 Student 엔티티가 있다고 해보자. 일반적인 컬럼이 존재한다. first name 과 last name import { Entity, PrimaryGeneratedColumn, Column } from "typeorm" @Entity() export class User { @PrimaryGeneratedColumn() id: string @Column() firstName: string @Column() lastName: string @Column() isActive: boolean } import { Entity, P.. 2023. 8. 31.
[TypeORM] Eager and Lazy Relations https://orkhan.gitbook.io/typeorm/docs/eager-and-lazy-relations Eager relations 즉시 관계는 엔티티를 로드할 때 마다 자동으로 같이 로드된다. import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from "typeorm" import { Question } from "./Question" @Entity() export class Category { @PrimaryGeneratedColumn() id: number @Column() name: string @ManyToMany((type) => Question, (question) => question.categories) questio.. 2023. 8. 31.
[TypeORM] What is EntityManager and Repository https://orkhan.gitbook.io/typeorm/docs/working-with-entity-manager https://orkhan.gitbook.io/typeorm/docs/working-with-repository What is EntityManager EntityManager를 사용하여 entity를 관리할 수 있다. EntityManager는 단지 단일 공간 내에 모든 엔티티 레포지토리의 집합이다. 데이터소스를 통해 entityManager에 접근할 수 있다. import { DataSource } from "typeorm" import { User } from "./entity/User" const myDataSource = new DataSource(/*...*/) const u.. 2023. 8. 31.
[TypeORM] Working with DataSource https://orkhan.gitbook.io/typeorm/docs/data-source What is DataSource 데이터 소스를 설정해야 DB와 소통이 가능하다. 데이터베이스 연결 설정 정보를 가지고 잇고 초기 데이터베이스 연결이나 커넥션 풀 설립을 담당한다. 초기 연결 설립, 커넥션 풀 설립을 위해 initialize 메소드를 호출해야 한다. destroy 메소드가 불릴 때, 연결이 끊긴다. 일반적으로 어플리케이션 bootstrap에서 initialize 메소드를 호출한다. DB와 완전히 처리가 끝난 이후에 destroy를 호출한다. 예를 들어 백엔드 개발을 하고 있고 서버가 닫히지 않는다면, destroy를 절대 호출되지 않는다. Creating a new DataSource 데이타소스 인.. 2023. 8. 30.
[TypeORM] DataSource API https://orkhan.gitbook.io/typeorm/docs/data-source-api dataSource.isInitialized 데이터소스 초기화와 데이터베이스 연결, 커넥션 풀 여부 dataSource.driver 데이터 소스에서 사용된 기본 DB 드라이버 dataSource.manager 엔티티 매니저 dataSource.mongoManager 몽고DB 매니저 await dataSource.initialize() 데이터소스 초기화, DB 커넥션 풀을 연다. await dataSource.destroy() 데이터 소스 종료 및 모든 DB커넥션을 종료한다. 주로 어플리케이션 종료시 사용 await dataSource.synchronize() 데이터베이스 스키마를 동기화한다. 데이터소스 옵션.. 2023. 8. 30.