typeorm-typedi-extensions
typeorm-typedi-extensions copied to clipboard
Allow @InjectRepository with a function
I have some circular depended entities (User and Token) and when I injecting Repository<User> and Repository<Token> in some class, User is undefined on the moment of the class construction.
class SomeController {
@InjectRepository(Token)
private tokenRepo: Repository<Token>
@InjectRepository(User) // User is undefined, so error is occured
private userRepo: Repository<User>
}
Error message: Missing "entityType" parameter of "@InjectRepository" decorator for a ...
If you allow to provide functions that returns class type, it will solve this problem:
@InjectRepository(of => Token) // like in TypeDI: @Inject(type => SomeService)
private tokenRepo: Repository<Token>
@InjectRepository(of => User)
private userRepo: Repository<User>
I would greatly appreciate this functionality as well!
are there any workarounds for solving the circular dependency issue?
Any updates on this issue?
I ran into this as well. To resolve, I patched the inject repository by checking if the metadata exists by calling connection.hasMetadata for the entity type. If not, I assume the entity is a function returning the entity type and I use the return value as entity type. It was a bit tricky to figure out if the func is an entity or a function returning an entity. Is there a better way to do this?
I don't know if I should spent time creating a pull request as #37 is also still open.
Is this something you can advise on @MichalLytek as you've approved #37?
I have some circular depended entities (User and Token) and when I injecting Repository<User> and Repository<Token> in some class, User is undefined on the moment of the class construction.
Do you have every entity and controller in different files?
are there any workarounds for solving the circular dependency issue?
@afiorito This article may help - https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de
Not to pollute the comments, but +1 to the article that @udayrajMT linked. We have been using the internal.ts pattern with typeorm + type-graphql for months in production without any issue and we haven't run into a single circular dependency issue since.