#MicroORM and Multiple Connections

8 messages · Page 1 of 1 (latest)

opal anvil
#

I'm looking in MicroORM features and I faced with strange (for me) case. If I do the following in the app.module.ts:

...MikroOrmModule.forRoot([
  {
    // https://github.com/mikro-orm/nestjs?tab=readme-ov-file#multiple-database-connections
    contextName: 'sqlite',
    registerRequestContext: false,
    entities: [Barcode],
    dbName: 'scanner_db.sqlite3',
    driver: SqliteDriver,
    debug: true,
  },
  {
    contextName: 'mongo',
    clientUrl:
      'mongodb://root:dockercompose@mongo:27017/?retryWrites=true&w=majority',
    registerRequestContext: false,
    // allowGlobalContext: true,
    entities: [Barcode2],
    dbName: 'devcontainer',
    driver: MongoDriver,
    debug: true,
  },
]),

My controller/service works fine, but if I want to read clientUrl and dbName from config then I'm doing this:

...MikroOrmModule.forRoot([
  {
    // https://github.com/mikro-orm/nestjs?tab=readme-ov-file#multiple-database-connections
    contextName: 'sqlite',
    registerRequestContext: false,
    entities: [Barcode],
    dbName: 'scanner_db.sqlite3',
    driver: SqliteDriver,
    debug: true,
  },
]),
MikroOrmModule.forMiddleware(),
MikroOrmModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => ({
    // https://github.com/mikro-orm/nestjs?tab=readme-ov-file#multiple-database-connections
    contextName: 'mongo',
    clientUrl: configService.getOrThrow<string>('MONGO_URI'),
    registerRequestContext: false,
    // allowGlobalContext: true,
    entities: [Barcode2],
    dbName: configService.getOrThrow<string>('MONGO_DB'),
    driver: MongoDriver,
    debug: true,
  }),
}),
#

Nest fails to start with the following:

[Nest] 63815  - 02/11/2026, 5:10:14 AM   ERROR [ExceptionHandler] UnknownDependenciesException [Error]: Nest can't resolve dependencies of the Barcode2Repository_mongo (?). Please make sure that the argument "mongo_EntityManager" at index [0] is available in the MikroOrmModule context.

Potential solutions:
- Is MikroOrmModule a valid NestJS module?
- If "mongo_EntityManager" is a provider, is it part of the current MikroOrmModule?
- If "mongo_EntityManager" is exported from a separate @Module, is that module imported within MikroOrmModule?
  @Module({
    imports: [ /* the Module containing "mongo_EntityManager" */ ]
  })
#

Also reading .env works as expected with Mongoose approach:

MongooseModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (configService: ConfigService) => ({
    uri: configService.getOrThrow<string>('MONGO_URI'),
    dbName: configService.getOrThrow<string>('MONGO_DB'),
  }),
}),
#

So my qustions is how I do:

Please make sure that the argument "mongo_EntityManager" at index [0] is available in the MikroOrmModule context.

#

Probably I can't do forRoot() and forRootAsync() at the same time?

empty kelp
opal anvil
#

You mean MicroORM community on Slack / Discord?

empty kelp
#

Discord preferably