Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add driver option to get around issues with useFactory and inject #204

Merged
merged 2 commits into from
Jan 28, 2025

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Jan 28, 2025

When you try to inject the EntityManager or MikroORM symbols exported from the driver package, Nest.js needs to be aware of those typed. In other words, those driver specific exports need to be specifically registered in the DI container. This module uses automated discovery of the driver type in order to do that, but it fails to work when you use useFactory which requires some dependencies.

Instead of relying on this discovery, you can provide the driver type explicitly:

@Module({
  imports: [
    MikroOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => configService.getOrThrow(ConfigKey.ORM),
      inject: [ConfigService],
      driver: PostgreSqlDriver,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Closes #184
Closes #195

B4nan added 2 commits January 28, 2025 19:43
…`inject`

When you try to inject the `EntityManager` or `MikroORM` symbols exported from the driver package, Nest.js needs to be aware of those typed. In other words, those driver specific exports need to be specifically registered in the DI container. This module uses automated discovery of the driver type in order to do that, but it fails to work when you use `useFactory` which requires some dependencies.

Instead of relying on this discovery, you can provide the driver type explicitly:

```typescript
@module({
  imports: [
    MikroOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => configService.getOrThrow(ConfigKey.ORM),
      inject: [ConfigService],
      driver: PostgreSqlDriver,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
```
@B4nan B4nan merged commit e37fd42 into master Jan 28, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PNP & MikroOrm + NestJS
1 participant