[Database][TypeORM] findOneBy vs findOne 차이

JIEUM KIM·2025년 8월 22일

Database

목록 보기
7/9

findOne

  • where 조건만 단순히 전달할 때 사용 (ID값이나 조건 객체를 모두 받아 혼동 가능)
  • 관계로드(relation), select, order같은 옵션 못 씀
const user = await userRepository.findOne(1); // ID로 검색
const user = await userRepository.findOne({ name: "John" }); // 조건으로 검색

findOneBy

  • where외에도 relation/select/order등 다양한 옵션 설정 가능
  • 응답 모양 제어가 필요할땐 findOne이 더 유리
const user = await userRepository.findOneBy({ id: 1 });
const user = await userRepository.findOneBy({ name: "John" });

예시

// 복잡한 쿼리는 findOne에 옵션 객체 사용
const user = await userRepository.findOne({
  where: { name: "John" },
  relations: ["posts"],
  order: { createdAt: "DESC" }
});

// findOneBy는 단순한 조건만 처리
const user = await userRepository.findOneBy({ name: "John" });

0개의 댓글