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

Allow @InjectRepository with a function #36

Open
maksimkurb opened this issue Mar 19, 2019 · 8 comments
Open

Allow @InjectRepository with a function #36

maksimkurb opened this issue Mar 19, 2019 · 8 comments
Labels
type: feature Issues related to new features.

Comments

@maksimkurb
Copy link

maksimkurb commented Mar 19, 2019

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>
@smithki
Copy link

smithki commented May 8, 2019

I would greatly appreciate this functionality as well!

@afiorito
Copy link

afiorito commented Jun 3, 2019

are there any workarounds for solving the circular dependency issue?

@glentakahashi
Copy link

Any updates on this issue?

@martijndeh
Copy link

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.

@martijndeh
Copy link

Is this something you can advise on @MichalLytek as you've approved #37?

@MichalLytek
Copy link
Contributor

I have some circular depended entities (User and Token) and when I injecting Repository and Repository in some class, User is undefined on the moment of the class construction.

Do you have every entity and controller in different files?

@udayrajMT
Copy link

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

@glentakahashi
Copy link

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.

@NoNameProvided NoNameProvided added the type: feature Issues related to new features. label Jan 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Issues related to new features.
Development

No branches or pull requests

8 participants