Skip to content

Commit

Permalink
EntityModule로 entity 객체 관리 및 모듈화
Browse files Browse the repository at this point in the history
  • Loading branch information
masonJS committed Nov 3, 2023
1 parent f955951 commit 6f16cc4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/config/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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',
Expand Down
21 changes: 21 additions & 0 deletions src/entity/domain/EntityModule.ts
Original file line number Diff line number Diff line change
@@ -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 {}
2 changes: 2 additions & 0 deletions test/getSqliteMikroOrmModule.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -11,6 +12,7 @@ export function getSqliteMikroOrmModule() {
schemaGenerator: {
createForeignKeyConstraints: false,
},
entities: [EntityModule],
autoLoadEntities: true,
allowGlobalContext: true,
debug: false,
Expand Down
12 changes: 2 additions & 10 deletions test/integration/achievement/AchievementService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
11 changes: 2 additions & 9 deletions test/integration/badhabit/BadhabitService.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
Expand Down

0 comments on commit 6f16cc4

Please sign in to comment.