Skip to content

Could not resolve MikroORM #213

Closed
Closed

Description

@klerick

Describe the bug
If use context, and try using forFeature with this context, module cannot resolve MicroORM in MikroOrmMiddleware

Stack trace

/project/node_modules/@nestjs/core/injector/injector.js:262
            throw new unknown_dependencies_exception_1.UnknownDependenciesException(wrapper.name, dependencyContext, moduleRef, { id: wrapper.id });
                  ^

Error: Nest can't resolve dependencies of the MikroOrmMiddleware (?). Please make sure that the argument MikroORM at index [0] is available in the MikroOrmCoreModule context.

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

To Reproduce
Steps to reproduce the behavior:

import {microOrmConfig} from './config';
const contextName = 'default'
@Module({
  imports: [MikroOrmModule.forFeature([], contextName)],
})
class Test {}

@Module({
  imports: [
    MikroOrmModule.forRoot({
...microOrmConfig,
contextName
}),

Posible solution
Create function which return provider for current context

export function ContextMicroOrmProvider(
  contextName: string
): FactoryProvider<MikroORM> {
  return {
    provide: ORM_CONTEXT,
    useFactory: (mikroORM: MikroORM) => mikroORM,
    inject: [connectionName ? getMikroORMToken(contextName) : MikroORM],
  };
}

@Injectable()
export class MikroOrmMiddleware implements NestMiddleware {

  @inject(ORM_CONTEXT) private readonly orm!: MikroORM

  use(req: unknown, res: unknown, next: (...args: any[]) => void) {
    RequestContext.create(this.orm.em, next);
  }

}

Activity

  1. locked and limited conversation to collaborators on Feb 26, 2025
  2. converted this issue into a discussion #214 on Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions