TypeORM

임성준·2022년 5월 19일
0
post-thumbnail

TypeORM

타입 ORM은 NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, Electron platforms 실행할 수 있는 ORM(Object Relational Mapping) 이며 타입스크립트와 자바스크립트(ES5, ES6, ES7, ES8)와 함께 사용할 수 있다. 이 프로그램의 목표는 항상 최신 JavaScript 기능을 지원하며, 개인 소규모 응용프로그램부터 데이터베이스가 여러 개인 대규모 엔터프라이즈 응용프로그램까지 데이터베이스를 사용하는 모든 종류의 응용프로그램을 개발하는 데 도움이 되는 추가 기능을 제공하는 것입니다.

TypeORM은 현재 존재하는 다른 모든 JavaScript ORM과 달리 ActiveRecord와 DataMapper 패턴을 모두 지원하므로 고품질과 유연성을가지고 결합되며 확장이 가능하고 유지보수가 가능한 애플리케이션을 가장 생산적인 방식으로 작성할 수 있습니다.

타입 ORM은 Hibernate, Doctrine, Entity 프레임워크와 같은 다른 ORM의 영향을 많이 받는다.

💎 개인 정리 : 자바스크립트를 통해 쉽게 데이터베이스를 관리

원문
TypeORM is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large scale enterprise applications with multiple databases.

TypeORM supports both Active Record and Data Mapper patterns, unlike all other JavaScript ORMs currently in existence, which means you can write high quality, loosely coupled, scalable, maintainable applications the most productive way.

TypeORM is highly influenced by other ORMs, such as Hibernate, Doctrine and Entity

설치 및 사용 🕹

설치 yarn add

설치

  • yarn add typeorm

연결

export const AppDataSource = new DataSource({
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "test",
    password: "test",
    database: "test",
    synchronize: true,
    logging: true,
    entities: [Post, Category],
    subscribers: [],
    migrations: [],
})

💎  사용
Active Record 패턴

  • 모델 자체에 쿼리 메소드를 정의하고 모델의 메소드를 사용하여 객체를 저장, 제거, 불러오기 방식을 구현
  const user = new User()
  user.firstName = "Timber"
  user.lastName = "Saw"
  user.age = 25
  await user.save()
  const allUsers = await User.find()
  const firstUser = await User.findOneBy({
      id: 1,
  })
  const timber = await User.findOneBy({
      firstName: "Timber",
      lastName: "Saw"
  }
  await timber.remove()

💎  사용
Data Mapper 패턴

  • 분리된 클래스에 쿼리 메소드를 정의하는 방식이며, Repository를 이용하여 객체를 저장, 제거, 불러오기를 구현한다. Active Record 패턴과 차이점은 모델에 접근하는 방식이 아닌 Repository에서 데이터에 접그하는 방식을 사용한다는 것이다.
import { Entity, Column, PrimaryColumn } from "typeorm"

@Entity()
export class Photo {
    @PrimaryColumn()
    id: number

    @Column()
    name: string

    @Column()
    description: string

    @Column()
    filename: string

    @Column()
    views: number

    @Column()
    isPublished: boolean
}
profile
오늘도 공부 📖🌙

0개의 댓글