Skip to content

Commit

Permalink
feat: allow passing an array of options into forRoot
Browse files Browse the repository at this point in the history
Since MikroORM v6.4, you can also define [multiple configurations](https://mikro-orm.io/docs/quick-start#configuration-file-structure) as part of a single ORM config. If you want to use such a combined config file, you need to destructure the result, since it will be also an array:

```typescript
@module({
  imports: [
    // `config` exports an array of configs
    ...MikroOrmModule.forRoot(config),
    MikroOrmModule.forMiddleware()
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```

Closes #202
  • Loading branch information
B4nan committed Jan 28, 2025
1 parent 98171c4 commit 952613f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ You can define multiple database connections by registering multiple `MikroOrmMo
export class AppModule {}
```

Since MikroORM v6.4, you can also define [multiple configurations](https://mikro-orm.io/docs/quick-start#configuration-file-structure) as part of a single ORM config. If you want to use such a combined config file, you need to destructure the result, since it will be also an array:

```typescript
@Module({
imports: [
// `config` exports an array of configs
...MikroOrmModule.forRoot(config),
MikroOrmModule.forMiddleware()
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
```

To access different `MikroORM`/`EntityManager` connections you have to use the new injection tokens `@InjectMikroORM()`/`@InjectEntityManager()` where you are required to pass the `contextName` in:

```ts
Expand Down
17 changes: 15 additions & 2 deletions src/mikro-orm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
MikroOrmModuleFeatureOptions,
MikroOrmModuleSyncOptions,
MikroOrmMiddlewareModuleOptions,
MaybePromise,
} from './typings';

@Module({})
Expand All @@ -23,11 +24,23 @@ export class MikroOrmModule {
MikroOrmEntitiesStorage.clear(contextName);
}

static forRoot(options?: MikroOrmModuleSyncOptions): DynamicModule | Promise<DynamicModule> {
static forRoot(options?: MikroOrmModuleSyncOptions): MaybePromise<DynamicModule>;
static forRoot(options?: MikroOrmModuleSyncOptions[]): MaybePromise<DynamicModule>[];
static forRoot(options?: MikroOrmModuleSyncOptions | MikroOrmModuleSyncOptions[]): MaybePromise<DynamicModule> | MaybePromise<DynamicModule>[] {
if (Array.isArray(options)) {
return options.map(o => MikroOrmCoreModule.forRoot(o));
}

return MikroOrmCoreModule.forRoot(options);
}

static forRootAsync(options: MikroOrmModuleAsyncOptions): DynamicModule | Promise<DynamicModule> {
static forRootAsync(options: MikroOrmModuleAsyncOptions): MaybePromise<DynamicModule>;
static forRootAsync(options: MikroOrmModuleAsyncOptions[]): MaybePromise<DynamicModule>[];
static forRootAsync(options: MikroOrmModuleAsyncOptions | MikroOrmModuleAsyncOptions[]): MaybePromise<DynamicModule> | MaybePromise<DynamicModule>[] {
if (Array.isArray(options)) {
return options.map(o => MikroOrmCoreModule.forRoot(o));
}

return MikroOrmCoreModule.forRootAsync(options);
}

Expand Down
2 changes: 2 additions & 0 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { AnyEntity, EntityName as CoreEntityName, EntitySchema, ForkOptions
import type { MiddlewareConsumer, ModuleMetadata, Scope, Type } from '@nestjs/common';
import type { AbstractHttpAdapter } from '@nestjs/core';

export type MaybePromise<T> = T | Promise<T>;

export interface NestMiddlewareConsumer extends MiddlewareConsumer {
httpAdapter: AbstractHttpAdapter;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mikro-orm.module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('MikroORM Module', () => {
it('forFeature should return repository', async () => {
const module = await Test.createTestingModule({
imports: [
MikroOrmModule.forRoot(testOptions),
...MikroOrmModule.forRoot([testOptions]),
MikroOrmModule.forFeature([Foo]),
],
}).compile();
Expand Down

0 comments on commit 952613f

Please sign in to comment.