nestjs / typeorm

TypeORM module for Nest framework (node.js) 🍇

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom repositories not bound to entity can't be injected using forFeature

Ginden opened this issue · comments

I'm submitting a...

  • Regression
  • Bug report
  • Feature request
  • Documentation issue or request

Current behavior

Custom repositories not bound to entity (it's explicitly allowed by TypeORM documentation) can't be injected using TypeOrmModule.forFeature([NonEntityRepository]).

Expected behavior

Custom repositories not bound to entity are usable.

Minimal reproduction of the problem with instructions

@Injectable()
@EntityRepository()
export class FooRepository {
     constructor(public readonly manager: EntityManager) {}
}

@Injectable()
export class FooService {
  constructor(
    private readonly fooRepository: FooRepository,
  ) {}
}

@Module({
  imports: [
    TypeOrmModule.forFeature([FooRepository])
  ],
  controllers: [FooController],
  providers: [FooService],
})
export class FooModule {
}

What is the motivation / use case for changing the behavior?

Actually using TypeORM feature.

Environment

Nest version: 7.6.15

extends Repository is required (as shown in this example https://docs.nestjs.com/techniques/database#custom-repository). Otherwise, this package doesn't know that the getCustomRepository should be called (and calls getRepository instead):

https://github.com/nestjs/typeorm/blob/master/lib/typeorm.providers.ts#L17-L24

We could technically retrieve typeorm metadata but this would bring unnecessary complexity to this package (this has been discussed in older issues, see history #143 (comment))