From 6f16cc4fffb78f0d48cd3bc9f31dae577abd0515 Mon Sep 17 00:00:00 2001 From: minwoo Date: Fri, 3 Nov 2023 22:55:47 +0900 Subject: [PATCH] =?UTF-8?q?EntityModule=EB=A1=9C=20entity=20=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EA=B4=80=EB=A6=AC=20=EB=B0=8F=20=EB=AA=A8=EB=93=88?= =?UTF-8?q?=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/mikro-orm.config.ts | 5 ++--- src/entity/domain/EntityModule.ts | 21 +++++++++++++++++++ test/getSqliteMikroOrmModule.ts | 2 ++ .../achievement/AchievementService.spec.ts | 12 ++--------- .../badhabit/BadhabitService.spec.ts | 11 ++-------- 5 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 src/entity/domain/EntityModule.ts diff --git a/src/config/mikro-orm.config.ts b/src/config/mikro-orm.config.ts index d340bf0..5210ec7 100644 --- a/src/config/mikro-orm.config.ts +++ b/src/config/mikro-orm.config.ts @@ -1,9 +1,9 @@ -import { HabitBadhabit } from 'src/entity/domain/habitBadhabit/HabitBadhabit.entity'; import { CustomMigrationGenerator } from './CustomMigrationGenerator'; import { CustomNamingStrategy } from './CustomNamingStrategy'; import { MikroOrmModuleOptions } from '@mikro-orm/nestjs'; import { ReflectMetadataProvider } from '@mikro-orm/core'; import * as dotenv from 'dotenv'; +import { EntityModule } from '../entity/domain/EntityModule'; dotenv.config(); @@ -17,13 +17,12 @@ const config: MikroOrmModuleOptions = { port: process.env.PORT ? Number(process.env.PORT) : 5432, metadataProvider: ReflectMetadataProvider, autoLoadEntities: true, - entities: ['../entity/domain', HabitBadhabit], - entitiesTs: ['../entity/domain', HabitBadhabit], namingStrategy: CustomNamingStrategy, allowGlobalContext: true, // 개발 환경이라면 true schemaGenerator: { createForeignKeyConstraints: false, }, + entities: [EntityModule], migrations: { disableForeignKeys: true, path: '../../scripts/migrations', diff --git a/src/entity/domain/EntityModule.ts b/src/entity/domain/EntityModule.ts new file mode 100644 index 0000000..af15f8b --- /dev/null +++ b/src/entity/domain/EntityModule.ts @@ -0,0 +1,21 @@ +import { Module } from '@nestjs/common'; +import { MikroOrmModule } from '@mikro-orm/nestjs'; +import { Achievement } from './achievement/Achievement.entity'; +import { Badhabit } from './badhabit/Badhabit.entity'; +import { Habit } from './habit/Habit.entity'; +import { HabitBadhabit } from './habitBadhabit/HabitBadhabit.entity'; +import { User } from './user/User.entity'; + +@Module({ + imports: [ + MikroOrmModule.forFeature([ + Badhabit, + Habit, + Achievement, + HabitBadhabit, + User, + ]), + ], + exports: [MikroOrmModule], +}) +export class EntityModule {} diff --git a/test/getSqliteMikroOrmModule.ts b/test/getSqliteMikroOrmModule.ts index 38bbf6e..1d9367a 100644 --- a/test/getSqliteMikroOrmModule.ts +++ b/test/getSqliteMikroOrmModule.ts @@ -1,6 +1,7 @@ import { MikroOrmModule } from '@mikro-orm/nestjs'; import { TsMorphMetadataProvider } from '@mikro-orm/reflection'; import { CustomNamingStrategy } from 'src/config/CustomNamingStrategy'; +import { EntityModule } from '../src/entity/domain/EntityModule'; export function getSqliteMikroOrmModule() { return MikroOrmModule.forRoot({ @@ -11,6 +12,7 @@ export function getSqliteMikroOrmModule() { schemaGenerator: { createForeignKeyConstraints: false, }, + entities: [EntityModule], autoLoadEntities: true, allowGlobalContext: true, debug: false, diff --git a/test/integration/achievement/AchievementService.spec.ts b/test/integration/achievement/AchievementService.spec.ts index 3c7c09a..4777f00 100644 --- a/test/integration/achievement/AchievementService.spec.ts +++ b/test/integration/achievement/AchievementService.spec.ts @@ -12,13 +12,11 @@ import { AchievementRequest } from 'src/module/achievement/dto/AchievementReques import { Achievement } from 'src/entity/domain/achievement/Achievement.entity'; import { HabitApiModule } from 'src/module/habit/HabitApiModule'; import { HabitService } from 'src/module/habit/HabitService'; -import { AchievementEntityModule } from 'src/entity/domain/achievement/AchievementEntityModule'; import { HabitQueryRepository } from 'src/module/habit/HabitQueryRepository'; -import { HabitEntityModule } from 'src/entity/domain/habit/HabitEntityModule'; import { AchievementFactory } from 'test/factory/AchievementFactory'; import { faker } from '@mikro-orm/seeder'; import { DateTimeUtil } from 'src/entity/util/DateTimeUtil'; -import { BadhabitEntityModule } from 'src/entity/domain/badhabit/BadhabitEntityModule'; +import { EntityModule } from '../../../src/entity/domain/EntityModule'; describe('AchievementService', () => { let orm: MikroORM; @@ -29,13 +27,7 @@ describe('AchievementService', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [ - getSqliteMikroOrmModule(), - HabitApiModule, - AchievementEntityModule, - HabitEntityModule, - BadhabitEntityModule, - ], + imports: [getSqliteMikroOrmModule(), HabitApiModule, EntityModule], providers: [ AchievementService, AchievementQueryRepository, diff --git a/test/integration/badhabit/BadhabitService.spec.ts b/test/integration/badhabit/BadhabitService.spec.ts index ee723d8..3e9271d 100644 --- a/test/integration/badhabit/BadhabitService.spec.ts +++ b/test/integration/badhabit/BadhabitService.spec.ts @@ -1,10 +1,7 @@ import { MikroORM } from '@mikro-orm/core'; import { Test, TestingModule } from '@nestjs/testing'; import { Badhabit } from 'src/entity/domain/badhabit/Badhabit.entity'; -import { BadhabitEntityModule } from 'src/entity/domain/badhabit/BadhabitEntityModule'; -import { HabitEntityModule } from 'src/entity/domain/habit/HabitEntityModule'; import { TransactionService } from 'src/entity/transaction/TransactionService'; -import { BadhabitQueryRepository } from 'src/module/badhabit/BadhabitQueryRepository'; import { BadhabitService } from 'src/module/badhabit/BadhabitService'; import { getSqliteMikroOrmModule } from 'test/getSqliteMikroOrmModule'; @@ -14,12 +11,8 @@ describe('BadhabitService', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [ - getSqliteMikroOrmModule(), - BadhabitEntityModule, - HabitEntityModule, - ], - providers: [BadhabitService, BadhabitQueryRepository, TransactionService], + imports: [getSqliteMikroOrmModule()], + providers: [BadhabitService, TransactionService], }).compile(); orm = module.get(MikroORM);