Nest.js - MySQL 연결

김세겸·2022년 12월 29일
0

code-camp

목록 보기
6/10

TypeORM

  1. TypeORM 설치
  2. module 연결
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
      logging: true,
    }),
  ],
})
export class AppModule {}
  1. entity 파일 생성
@Entity()
export class test {
	@PrimaryGeneratedColum("option") // id값 생성 option : uuid - unique increment - 알아서 1씩 증가
    number: number;
    
    @Colum()
    name: string;
    
    @Colum()
    address: string;
}
  1. TypeModule 에서 entities에 추가 필요
  2. nest.js 실행 시 자동으로 테이블 생성해줌

0개의 댓글