InjectRepository with generic type T
See original GitHub issueI’m submitting a…
[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I would like to be able to inject a Repository with a generic type. Do you think it is possible?
Current behavior
@Injectable()
export class ResourcesService<T> {
constructor(
@InjectRepository(T) // <-- error happens here
private readonly repo: Repository<T>
) {}
}
Typescript error:
error TS2693: 'T' only refers to a type, but is being used as a value here.
This is the only way I can think of doing it, and it is not a valid way.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Nestjs How to extend service from Generic - Stack Overflow
You need to inject repository inside constructor, so: export function DataService<T>(entity: Constructor<T>): Type<IDataService<T>> { class ...
Read more >Implementing a Generic Repository Pattern Using NestJS
The code for AbstractRepository class is given below: You can add as many functionalities as you need. T represent each entity.
Read more >Best Way to Inject Repositories using TypeORM (NestJS)
Taking Class-Transformer & TypeORM in NestJS & comparing them with Laravel ... UserEntity as generic types to our base ModelRepository .
Read more >nestjs-generic-crud - npm Package Health Analysis - Snyk
In the past month we didn't find any pull request activity or change in issues status has been detected for the GitHub repository....
Read more >typeorm-typedi-extensions - npm
TypeScript icon, indicating that this package has built-in type ... have to create the class which extends the generic Repository<T> class ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Finally managed to do it, below is a code snippet you might find useful
Not you can extend you services with this as explained by @BrunnerLivio here https://github.com/nestjs/typeorm/issues/187#issuecomment-528910193
This is very nice. Can avoid a lot of boilerplate code.