๐Ÿš€ TypeORM

hwakyungChoiยท2021๋…„ 1์›” 11์ผ
0

TypeORM์˜ ์ •์˜

  • TypeORM์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด Node.js, React Native, Expo์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋Š” ORM์ž…๋‹ˆ๋‹ค.
  • ORM(Object Relational Mapping)์œผ๋กœ ๊ฐ์ฒด-๊ด€๊ณ„ ๋งคํ•‘์œผ๋กœ ๊ฐ์ฑ„์™€ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ๊ฒƒ์„ ๋œปํ•ฉ๋‹ˆ๋‹ค.
//TypeORM์˜ ๋ชจ๋“ˆ ์˜ˆ์‹œ
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    age: number;

}

// ๋„๋ฉ”์ธ ๋กœ์ง ์˜ˆ์‹œ

const repository = connection.getRepository(User);

const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.age = 25;
await repository.save(user);

const allUsers = await repository.find();
const firstUser = await repository.findOne(1); // find by id
const timber = await repository.findOne({ firstName: "Timber", lastName: "Saw" });

await repository.remove(timber);

TypeORM์˜ ์žฅ์ 

  • ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ง€์›ํ•ด์ค๋‹ˆ๋‹ค.(MariaDB,NOSQL ๋“ฑ)
  • Active Record์™€ Data MapperํŒจํ„ด์„ ์ง€์›ํ•ด์ค๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ์— entities์™€ ํ•จ๊ป˜ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

TypeORM์˜ ๋‹จ์ 

  • ์‹คํ–‰ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค๋Š” ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ์กฐ๊ธˆ ๋ณต์žกํ•˜๋‹ค๋Š” ์ ๋„ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

TypeORM์˜ ํ–ฅํ›„ ๋ฏธ๋ž˜

  • ์ฃผ๊ฐ„ ๋‹ค์šด๋กœ๋“œ ์ˆ˜๊ฐ€ 40๋งŒ๊ฑด์ด ๋„˜์œผ๋ฉด์„œ ์ง€์†์ ์œผ๋กœ ์ฆ๊ฐ€๋˜๊ณ  ์žˆ๋Š” ์ถ”์„ธ์ž…๋‹ˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์•„์ง๊นŒ์ง€ Sequelize๋ผ๋Š” ๋Œ€ํ‘œ์ ์ธ ORM๊ณผ ๋น„๊ต๋˜๊ณ  ์žˆ์œผ๋ฉฐ ์•„์ง ๋”ฐ๋ผ์žก๊ธฐ์—๋Š” ์กฐ๊ธˆ ๋” ์ง€์ผœ๋ด์•ผํ•  ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€