인프런 커뮤니티 질문&답변
DB 연결 부분
작성
·
231
0
안녕하세요 제로초님 DB 연결하는 부분에서 공식문서마다 다른 부분이 있어서 여쭤봅니다.
https://orkhan.gitbook.io/typeorm/#creating-a-connection-to-the-database 에 의하면
import "reflect-metadata";
import { createConnection } from "typeorm";
import { Photo } from "./entity/Photo";
createConnection({
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
entities: [
Photo
],
synchronize: true,
logging: false
}).then(connection => {
// here you can start to work with your entities
}).catch(error => console.log(error));
이런 형태로 접속을 하고
https://docs.nestjs.com/techniques/database#typeorm-integration 에 의하면
JS
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,
}),
],
})
export class AppModule {}
이런식으로 연결하고
제로초님 강의에서는
dotenv.config();
const config: TypeOrmModuleOptions = {
type: 'mysql',
host: 'localhost',
port: 3306,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
entities: [
ChannelChats,
ChannelMembers,
Channels,
DMs,
Mentions,
Users,
WorkspaceMembers,
Workspaces,
],
migrations: [__dirname + '/src/migrations/*.ts'],
cli: { migrationsDir: 'src/migrations' },
autoLoadEntities: true,
charset: 'utf8mb4',
synchronize: false,
logging: true,
keepConnectionAlive: true,
};
export = config;
이런식으로 연결을 하던데
혹시 어떠한 차이가 있는건지 궁금합니다.
답변 1
0
제로초(조현영)
지식공유자
공식문서나 제 방식이 맞습니다. 공식문서의 방식에서 설정 부분을 분리한 것이 제 방식이고요. 제 방식대로 분리해야 migration이나 seed를 할 수 있습니다.
첫 번째 방식은 nest가 typeorm을 파악하지 못하는 단점이 있습니다.





