I am attempting to create a specialized repository class that extends Repository in order to incorporate custom logging functions that can be utilized across all repositories. Here is the snippet of code:
user.service.ts:
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private readonly userRepository: BaseRepository<User>,
) {}
async update(id: string, data: UpdateUserDto): Promise<UpdateResult> {
return await this.userRepository.updateAndLog(id, data);
}
}
BaseRepository.ts:
import { Repository, UpdateResult } from 'typeorm';
export class BaseRepository<T> extends Repository<T> {
async updateAndLog(id: string, data: any): Promise<UpdateResult> {
const entity = await this.findOne(id as any);
const savedEntity = await this.update(id, data);
// log the data here
return savedEntity;
}
}
Therefore, the outcome of the function always displays:
[Nest] 13820 - 04/11/2023, 12:07:07 PM ERROR [ExceptionsHandler] this.userRepository.updateAndLog is not a function
I have carefully reviewed the typeorm documentation regarding custom repositories:
Additional resources from StackOverflow:
- How to do custom repository using TypeORM (MongoDB) in NestJS?
- NestJS/TypeORM: Can custom repository extend from another custom repository which is inside another project?
View the repository on github: https://github.com/typeorm/typeorm/issues/2097
Despite my efforts, the code provided above is not functioning as expected. Is there an issue I may have overlooked?