TypeORM-MySQL Migration Guide

gosuยท2024๋…„ 3์›” 20์ผ
0
post-thumbnail

TypeORM Migration Guide ๐Ÿšฅ

ORM์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉด ๋ฌผ๋ก  ํŽธํ•˜์ง€๋งŒ,
๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ ํ•˜๊ณ  ์—”ํ‹ฐํ‹ฐ๋ฅผ ์—ญ์œผ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด ์žˆ๋‹ค.
์ฆ‰, ํ…Œ์ด๋ธ” ์ƒ์„ฑ โžก๏ธ TypeORM Generator โžก๏ธ ์—”ํ‹ฐํ‹ฐ ์ˆ˜์ •

1. ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๐Ÿ“œ

a. Reverse Engineer

  • MySQL Workbench 8.0
  • Database - Reverse Engineer
  • ๋‹ค๋ฅธ ํ…Œ์ด๋ธ”์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์—ฐ๊ด€ ๊ด€๊ณ„๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

b. Forward Engineer

  • ์ž‘์—…์„ ๋‹ค ๋งˆ์ณค๋‹ค๋ฉด Database -Forward Engineer
  • ํ…Œ์ด๋ธ” ์ƒ์„ฑ SQL ๋ฌธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
-- -----------------------------------------------------
-- Table `test`.`test_reverse_copy`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`test_reverse_copy` (
  `test_reverse_copy_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `type` VARCHAR(20) CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`test_reverse_copy_id`))
ENGINE = InnoDB
AUTO_INCREMENT = 166
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
  • Forward Engineer๋ฅผ ๋‹ค ๋งˆ์ณค๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ฑ๊ณต์ ์œผ๋กœ ํ…Œ์ด๋ธ”์ด ์ƒ๊ธด ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

2. TypeORM Migration ๐ŸงŠ

a. typeorm-model-generator

  • ์—ฐ๊ฒฐ๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ƒํƒœ๋ฅผ ํ† ๋Œ€๋กœ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • ์—”ํ‹ฐํ‹ฐ๋Š” /output/entities ํด๋”์— ๋งŒ๋“ค์–ด์ง„๋‹ค.
npx typeorm-model-generator -h [ํ˜ธ์ŠคํŠธ ์ฃผ์†Œ] -p 3306 -d [๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„] -u [์œ ์ € ์ด๋ฆ„] -x [์ ‘์†ํŒจ์Šค์›Œ๋“œ] -e mysql
  • ์‘๋‹ต ๊ฒฐ๊ณผ
Need to install the following packages:
typeorm-model-generator@0.4.6
Ok to proceed? (y) y
npm WARN deprecated adal-node@0.2.4: This package is no longer supported. Please migrate to @azure/msal-node.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated @npmcli/move-file@1.1.2: This functionality has been moved to @npmcli/fs
typeorm-model-generator@0.4.6
[3:28:27 PM] Starting creation of model classes.
[3:28:27 PM] Typeorm model classes created.
npm notice
npm notice New minor version of npm available! 10.2.3 -> 10.5.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.5.0
npm notice Run npm install -g npm@10.5.0 to update!
npm notice

b. ์—”ํ‹ฐํ‹ฐ ํ™•์ธ

  • /output/entities/TestReverseCopy.ts
  • ์‹ค์ œ TypeORM์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์—”ํ‹ฐํ‹ฐ ํด๋”๋กœ ์˜ฎ๊ฒจ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity("test_reverse_copy", { schema: "rever_school" })
export class TestReverseCopy {
  @PrimaryGeneratedColumn({
    type: "bigint",
    name: "test_reverse_copy_id",
    unsigned: true,
  })
  testReverseCopyId: string;

  @Column("varchar", {
    name: "type",
    length: 20,
  })
  type: string;

  @Column("datetime", {
    name: "created_at",
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt: Date;

  @Column("datetime", {
    name: "updated_at",
    default: () => "CURRENT_TIMESTAMP",
  })
  updatedAt: Date;

  @Column("datetime", { name: "deleted_at", nullable: true })
  deletedAt: Date | null;
}

c. ์—”ํ‹ฐํ‹ฐ์— ํ•„์š” ์ œ์•ฝ์กฐ๊ฑด ์„ค์ •

  • @IsNumber()
  • @ApiProperty()
  • @IsString()

๋‹ค์Œ๊ณผ ๊ฐ™์ด, ํ•„์š”ํ•œ ์ œ์•ฝ ์กฐ๊ฑด์„ ์„ค์ •ํ•˜๊ณ , ์ •์ƒ์ ์œผ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋˜์—ˆ๋‹ค๋ฉด TypeORM๊ณผ ์—ฐ๋™ ๋œ๋‹ค.

profile
๊ฐœ๋ฐœ์ž ๋ธ”๋กœ๊ทธ ^0^

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

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด