Skip to content

Commit

Permalink
fix: ensure correct application shutdown with forRootAsync and multip…
Browse files Browse the repository at this point in the history
…le databases (#75)

* When using `forRootAsync` module with multiple databases on application shutdown it fails to find MikroORM and causes a shutdown error.

* undo yarn.lock changes

* make sure contextName is set for the orm

Co-authored-by: Steven Tsang <tsangst@gmail.com>
  • Loading branch information
tsangste and Steven Tsang committed May 14, 2022
1 parent 4609e5d commit 91b6faf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/mikro-orm.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export function createMikroOrmProvider(contextName?: string): Provider {
options = config as unknown as MikroOrmModuleOptions;
}

const mergedOptions = !options.contextName && contextName ? { ...options, contextName } : options;
return MikroORM.init(mergedOptions);
return MikroORM.init(options);
},
inject: [MIKRO_ORM_MODULE_OPTIONS],
};
Expand All @@ -49,7 +48,12 @@ export function createMikroOrmAsyncOptionsProvider(options: MikroOrmModuleAsyncO
if (options.useFactory) {
return {
provide: MIKRO_ORM_MODULE_OPTIONS,
useFactory: options.useFactory,
useFactory: (...args: any[]) => {
const factoryOptions = options.useFactory!(...args);
return options.contextName
? { contextName: options.contextName, ...factoryOptions }
: factoryOptions;
},
inject: options.inject || [],
};
}
Expand Down
8 changes: 5 additions & 3 deletions tests/mikro-orm.middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class TestController {

@Module({
imports: [
MikroOrmModule.forRoot({
MikroOrmModule.forRootAsync({
contextName: 'database1',
registerRequestContext: false,
...testOptions,
useFactory: () => ({
registerRequestContext: false,
...testOptions,
}),
}),
MikroOrmModule.forRoot({
contextName: 'database2',
Expand Down
8 changes: 6 additions & 2 deletions tests/mikro-orm.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ class ConfigService implements MikroOrmOptionsFactory {
constructor(@Inject('my-logger') private readonly logger: Logger) { }

createMikroOrmOptions(contextName?: string): Options {
return {
contextName,
const options = {
...testOptions,
logger: this.logger.log.bind(this.logger),
};

return contextName ? { contextName, ...options } : options;
}

}
Expand Down Expand Up @@ -105,6 +106,7 @@ describe('MikroORM Module', () => {

const orm = module.get<MikroORM>(MikroORM);
expect(orm).toBeDefined();
expect(orm.config.get('contextName')).toBe('default');
expect(module.get<EntityManager>(EntityManager)).toBeDefined();
await orm.close();
});
Expand All @@ -119,6 +121,7 @@ describe('MikroORM Module', () => {

const orm = module.get<MikroORM>(MikroORM);
expect(orm).toBeDefined();
expect(orm.config.get('contextName')).toBe('default');
expect(module.get<EntityManager>(EntityManager)).toBeDefined();
await orm.close();
});
Expand All @@ -137,6 +140,7 @@ describe('MikroORM Module', () => {

const orm = module.get<MikroORM>(MikroORM);
expect(orm).toBeDefined();
expect(orm.config.get('contextName')).toBe('default');
expect(module.get<EntityManager>(EntityManager)).toBeDefined();
await orm.close();
});
Expand Down

0 comments on commit 91b6faf

Please sign in to comment.