Database/TypeORM
EntityRepository - deprecated 되었다.
wam
2024. 8. 2. 20:38
TypeORM 0.3.0 부터 EntityRepository가 deprecated 되었다.
커스텀 Repository으로 구현하기
// user.repository.ts
@Injectable()
export class UsersRepository extends Repository<UsersEntity> {
constructor(private dataSource: DataSource) {
super(UsersEntity, dataSource.createEntityManager());
}
async getById(id: string) {
return this.findOne({ where: { id } });
}
// ...
}
The repository is then injected into the service.
// user.service.ts
export class UserService {
constructor(private readonly userRepository: UserRepository) {}
async getById(id: string): Promise<User> {
return this.userRepository.getById(id);
}
// ...
}
and the module has imports for the feature and the repository as a provider.
// user.module.ts
@Module({
imports: [
TypeOrmModule.forFeature([UserEntity])],
// ...
],
providers: [UserService, UserRepository],
// ...
})
export class UserModule {}