I wanted to use custom repositories in TypeORM, but I found that @EntityRepository(User)
was deprecated. I ended up with this solution, which is ultimately working for me. However, I want to know if it is optimal or if it is redundant, inefficient, or something like that.
import { Repository } from 'typeorm';
import { User } from '../entities/user.entity';
import { UnauthorizedException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
export class UserRepository extends Repository<User> {
@InjectRepository(User)
userRepository: Repository<User>;
async findByCodeOrEmail(userCredentials: string): Promise<User | undefined> {
try {
const user = await this.userRepository.findOne({
where: [
{ code: userCredentials },
{ email: userCredentials }
]
});
return user;
} catch (error) {
console.error('Error in UserRepository:', error);
throw new UnauthorizedException('Error con credenciales');
}
}
}
I encountered deprecated functions for using custom repositories and found a solution that I am not sure is the best.
Web Projects Sun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.