Is it possible to use @InjectRepository without registering entities in sub-modules?

395 views Asked by At

Lets lay ive got a root module like:

@Module({
  imports: [MikroOrmModule.forRoot({
    metadataProvider: TsMorphMetadataProvider,
    entities: ['dist/**/*.entity.js'],
    entitiesTs: ['src/**/*.entity.ts'],
    type: 'postgresql' as const,
    baseDir: path.resolve(__dirname, '..'),
    ...        
  })],
})
export class AppModule {}

With this setting I can inject EntityManager, but @InjectRepository(User) is not finding the provider.

@Injectable()
export class SomeService {

  constructor(@InjectRepository(user) userRepository: EntityRepository<User>) {}

}

If I also insert MikroOrmModule.forFeature([User]) into the root module it does not work. If I move the entity into a submodule and use .forFeature there (just like the example in the docs say) it works.

Is it possible to have @InjectRepository() working without the need for sub modules and manual referencing? Since Im already finding the entities with blobs, manually referencing them again seems to defeat the purpose of the blob pattern.

2

There are 2 answers

1
Jay McDoniel On

The blob pattern is to tell the ORM what all entities there are and what to have connections for. The forFeature pattern is to register all of the entities with Nest so that Nest can create injection tokens for them. As they are separate processes, there isn't a way to use only one of them. Unless the MikroOrmModule implements something like Nest's autoLoadEntities (on TypeOrmModule and SequelizeModule), there won't be a way to use only the globs.

0
Patrik Radics On

Yes it is

module

import {Module} from '@nestjs/common';
import {PrismaMigrationService} from "../services/prisma.migration.service";
import {MikroOrmModule} from "@mikro-orm/nestjs";
import {subscribers} from "./subscribers/collector.subscriber";
import {entitiesArray} from "./collector";
import {MikroOrmService} from "./mikro-orm.service";


@Module({
    controllers: [],
    providers: [PrismaMigrationService,MikroOrmService,...subscribers],
    imports: [MikroOrmModule.forFeature((entitiesArray))],
})
export class MikroOrmCollectorModule {
}

service

import {Injectable, OnModuleInit} from "@nestjs/common";
import {InjectRepository} from "@mikro-orm/nestjs";
import {Author} from "./main.mikro.entity";
import {EntityRepository} from '@mikro-orm/core';
import {i18nSubscriber} from "./subscribers/i18nNormal.subscriber";
import {i18Normal2} from "./secondary.mikro.entity";


    @Injectable()
    export class MikroOrmService implements OnModuleInit {
        constructor(@InjectRepository(Author) public authorRepository: EntityRepository<Author>,
                    @InjectRepository(i18Normal2) public i18Normal2Repository: EntityRepository<i18Normal2>,
                    private i18nSubscriber: i18nSubscriber) {
    
        }
    
        async onModuleInit() {
        }
    }

and then you can just inject private readonly em: EntityManager to the controllers