Can not create custom repository from generic repository
mcd01 opened this issue · comments
I'm submitting a...
[ ] Regression
[x] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
It is not possible to extend the Repository
class in a general way, and then use the extended class for custom repositories as described in https://docs.nestjs.com/v7/techniques/database#custom-repository.
Expected behavior
We want to override / extend some general functionality in the Repository
class and then use the enriched class for creation of custom repositories.
Minimal reproduction of the problem with instructions
Say we want to extend the Repository
class, e.g.
export class ExtendedRepository<T extends BaseEntity> extends Repository<T> {
// override, for example, the save-method
}
where BaseEntity
is an entity with some basic properties, and all other entities are extending this class. We then want to use the extended repository for creation of custom repositories as described here https://docs.nestjs.com/v7/techniques/database#custom-repository, e.g.
@EntityRepository(Author)
export class AuthorRepository extends ExtendedRepository<Author> {}
with
@Entity()
export class Author extends BaseEntity {...}
We also then follow the missing two ingredients as described in the documentation, i.e.
- delegate instantiation responsibility to Nest
- inject the repository, e.g. into the AuthorService.
However, if we follow this approach, the injected repository ends up being corrupted, i.e. certain properties and functions are missing, and we cant use it. On the other hand, if we create the AuthorRepository
by directly extending from Repository<Author>
, there are no problems.
From the NestJS documentation and general knowledge about javascript / typescript and inheritance, this leaves us a little in the dark. Are we missing something or having an error in our code? Or, in general: Is what we want even possible? Basically, we just want to override some functionality and then create all real repositories by extending from the enriched one, instead of explicitly overriding functionality whenever we create a custom repository (e.g. AuthorRepository
).
Thanks for your help in advance!
Environment
Nest version: v7.0.7
For Tooling issues:
- Node version: v10.19.0
- Platform: Linux
Please provide a minimum reproduction repository.
Fixed with update to Nest version 8.0.0