question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MikroOrmModule.forRootAsync ignores MikroOrm provider useFactory function

See original GitHub issue

Describe the bug When importing MikroOrm with forRootAsync and using Factory provider: useFactory the given options are ignored and defaults are used instead. From the stack trace it looks like it is instantiating MikroOrm class directly instead of using useFactory from the MikroOrm custom provider that uses MikroOrm.init() method. https://github.com/mikro-orm/nestjs/blob/master/src/mikro-orm.providers.ts#L24 But these are my suppositions and I feel I’m missing something.

Stack trace

[Nest] 65   - 01/08/2021, 9:00:27 PM   [ExceptionHandler] No platform type specified, please fill in `type` or provide custom driver class in `driver` option. Available platforms types: [ 'mongo', 'mysql', 'mariadb', 'postgresql', 'sqlite' ] +30ms
Error: No platform type specified, please fill in `type` or provide custom driver class in `driver` option. Available platforms types: [ 'mongo', 'mysql', 'mariadb', 'postgresql', 'sqlite' ]
    at Configuration.validateOptions (/app/node_modules/@mikro-orm/core/utils/Configuration.js:170:13)
    at new Configuration (/app/node_modules/@mikro-orm/core/utils/Configuration.js:25:12)
    at new MikroORM (/app/node_modules/@mikro-orm/core/MikroORM.js:23:21)
    at Injector.instantiateClass (/app/node_modules/@nestjs/core/injector/injector.js:286:19)
    at callback (/app/node_modules/@nestjs/core/injector/injector.js:42:41)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Injector.resolveConstructorParams (/app/node_modules/@nestjs/core/injector/injector.js:114:24)
    at async Injector.loadInstance (/app/node_modules/@nestjs/core/injector/injector.js:46:9)
    at async Injector.loadProvider (/app/node_modules/@nestjs/core/injector/injector.js:68:9)
    at async Promise.all (index 4)

To Reproduce Simply use MikroOrmModule.forRootAsync() in imports[] in Nestjs AppModule. In my case I’m injecting ConfigService from Nestjs, but i don’t think this should change something.

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration]
    }),
    MikroOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        metadataProvider: TsMorphMetadataProvider,
        namingStrategy: EntityCaseNamingStrategy,
        entities: ["./dist/**/entities/*.entity.js"],
        entitiesTs: ["./src/**/entities/*.entity.ts"],
        type: "postgresql",
        dbName: configService.get("database.name"),
        host: configService.get("database.host"),
        port: configService.get("database.port"),
        user: configService.get("database.user"),
        password: configService.get("database.password")
      }),
      inject: [ConfigService]
    })
  ],
  controllers: [AppController],
  providers: [AppService, MikroORM]
})

Expected behavior Get a MikroOrm instance with provided configuration.

Versions

Dependency Version
node 14
typescript 3.7.4
mikro-orm/core 4.3.4
mikro-orm/nestjs 4.2.0
mikro-orm/postgresql 4.3.4
mikro-orm/reflection 4.3.4

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
B4nancommented, Jan 8, 2021

You can see that from the stack trace, it is caused by that because nest di is trying to create new instance itself. Remove it and let the nest mikro orm adapter create the instance.

0reactions
JhonFuenzalidacommented, Dec 20, 2022

I had the same error and I solved it by changing the type to drive

import { Module } from '@nestjs/common';
  import { ConfigModule, ConfigService } from '@nestjs/config';
  import { MikroOrmModule } from '@mikro-orm/nestjs';
  import { PostgreSqlDriver } from '@mikro-orm/postgresql';
  
  @Module({
    imports: [
      MikroOrmModule.forRootAsync({
        imports: [ConfigModule],
        inject: [ConfigService],
        useFactory: (configService: ConfigService) => ({
          driver: PostgreSqlDriver,
          dbName: configService.get<string>('DATABASE_NAME'),
          host: configService.get<string>('DATABASE_HOST'),
          port: configService.get<number>('DATABASE_PORT'),
          user: configService.get<string>('DATABASE_USER'),
          password: configService.get<string>('DATABASE_PASS'),
          entitiesTs: ['libs/entities/src/lib'],
          autoLoadEntities: true,
        }),
      }),
    ]
  })
  export class DatabaseModule {}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Metadata Providers | MikroORM
As part of entity discovery process, MikroORM uses so called MetadataProvider to get necessary type information about our entities' properties. We can also ......
Read more >
nestjs - How to use module with scope request and services
When set to module MikroOrmModule scope equal to request?, get error: ... Mikro orm have a middleware that use https://mikro-orm.io/docs/ ...
Read more >
Setup Nodejs API with Nestjs and Mikro-ORM
In this post, I guide you on how to create a Nestjs application, with the help of MikroORM, with Postgresql as database. Prerequisites....
Read more >
The nestjs from mikro-orm - Coder Social
MikroOrmModule.forRootAsync ignores MikroOrm provider useFactory function. Describe the bug. When importing MikroOrm with forRootAsync and using Factory ...
Read more >
API with NestJS #62. Introduction to MikroORM with PostgreSQL
We look into using PostgreSQL with an Object-relational mapping (ORM) library called MikroORM as an alternative to TypeORM.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found