-
When registering ...
MikroOrmModule.forRootAsync({
useFactory: (configService: ConfigService) => configService.getOrThrow(ConfigKey.ORM),
inject: [ConfigService],
})
... I think there is no way, how this could be done automatically in MikroOrmModule.forRootAsync in that case, but I came up with very simple solution - create alias module, which can user register manually: E.g.: mikro-orm-alias.module.ts for import { type DynamicModule, Module, Global } from '@nestjs/common'
import { MikroORM as PostgreSqlMikroORM, EntityManager as SqlEntityManager } from '@mikro-orm/postgresql'
import { MikroORM, EntityManager } from '@mikro-orm/core'
@Global()
@Module({})
export class MikroOrmAliasModule {
static forRoot(): DynamicModule {
return {
module: MikroOrmAliasModule,
providers: [
{
provide: PostgreSqlMikroORM,
useFactory: (mikroOrm: MikroORM) => mikroOrm,
inject: [MikroORM],
},
{
provide: SqlEntityManager,
useFactory: (entityManager: EntityManager) => entityManager,
inject: [EntityManager],
},
],
exports: [PostgreSqlMikroORM, SqlEntityManager],
}
}
} And than using in Root module: ...
MikroOrmAliasModule.forRoot(),
... Than driver specific injection in services works fine: import { RequestContext, MikroORM } from '@mikro-orm/postgresql'
...
constructor(private readonly orm: MikroOrm) {}
... I am thinking about, if it probably could be somehow embedded into MikroOrm, so user imports and registers driver specific alias module, so driver specific injection will work. Or probably it could be mentioned somewhere in documentation? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 5 replies
-
I am not sure how could we integrate this (unless we provide driver specific exports in the nest package too), but this is surely worth documenting. |
Beta Was this translation helpful? Give feedback.
-
Yup, probably driver specific export would be an overkill for the nest package 😄 |
Beta Was this translation helpful? Give feedback.
-
Maybe we could at least simplify this - all we need to know is the driver in use, from there we can infer everything. So instead of wrapping everything explicitly, we could just let the user pass in the driver in a separate option next to the MikroOrmModule.forRootAsync({
useFactory: (configService: ConfigService) => configService.getOrThrow(ConfigKey.ORM),
inject: [ConfigService],
driver: PostgreSqlDriver,
}) Then we can create the driver instance and the EM out of it. I also wanted to add public methods to the platform level to expose the |
Beta Was this translation helpful? Give feedback.
-
For the user, adding such a parameter could be quite simple, as well as the implementation itself. I'm definitely in favor. |
Beta Was this translation helpful? Give feedback.
-
does this mean we cannot use |
Beta Was this translation helpful? Give feedback.
-
This is now implemented in v6.1 via #204 I will enforce it in v7 and remove the |
Beta Was this translation helpful? Give feedback.
This is now implemented in v6.1 via #204
I will enforce it in v7 and remove the
tryRequire
quirk, removing it now would be breaking. For now, there is a warning when the autodetection fails.