https://springdream0406.tistory.com/163
Find Options
- 기본 옵션들@Get('users') getUsers() { return this.userRepository.find({ // 어떤 프로퍼티를 선택할지 // 기본은 모든 프로퍼티를 가져온다 // 만약에 select를 정의하지 않으면 // select를 정의하면 정의된 프로
springdream0406.tistory.com
https://springdream0406.tistory.com/164
Methods
@Post('sample') async sample() { // 모델에 해당되는 객체 생성 - 저장은 안함 const user1 = this.userRepository.create({ email: 'test@codefactory.ai', }); // 저장 const user2 = await this.userRepository.save({ email: 'test@codefactory.ai',
springdream0406.tistory.com
++ 위에 링크에 정리된게 더 잘 정리된거.
// Module 에서 import
imports: [TypeOrmModule.forFeature([Auth, User])],
// Service에 주입
@InjectRepository(Auth)
private readonly authRepository: Repository<Auth>,
// 다른 테이블도 작업하려면 똑같이
// @InjectRepository(테이블)
// private readonly authRepository: Repository<테이블>,
// 해줘야하는데, 한번에 여러게 하는건 지양한다고 함. 따라서 각각나눠서 service 만들고 처리
// 사용 예시 코드들
// findOne, where, relation
findOneByUserId({ user_id }: { user_id: string }): Promise<Auth> {
return this.authRepository.findOne({
where: { user_id },
relations: ['user'],
});
}
// oneToOne이나 ManyToOne 같이 연결된 얘들은 이렇게
findOneByUid({ user_id }: { user_id: string }): Promise<Auth> {
return this.authRepository.findOne({
where: {
user: { id: user_id },
},
relations: ['user'],
});
}
.save() => 등록/수정가능, 결과 받아올 수 있음, pk가 없으면 등록 / pk가 있으면 수정
.insert() => 등록, 결과 안받아옴
.update() => 수정, 결과 안받아옴
.create() => DB랑 상관없음. 등록을 위해서 빈 객체 만드는거
- 삭제
.deleted() // 실제 삭제
.softRemove() // id로만 삭제 가능, 여러id한번에 삭제 가능
.softDelete() // 다른 컬럼으로도 삭제 가능, 여러id한번에 삭제 불가능
// 결과
UpdateResult { generatedMaps: [], raw: [], affected: 1 }
// 리턴
const result = await 삭제()
return result.affected ? true : false;
- orderby, 배열 검색
// 배열로 검색, orderby
findApplyFarmByUserId({
getMyFarmApplyInput,
}: IFarmServiceGetMyFarmApplyInput): Promise<Farm_Application[]> {
return this.farm_ApplicationRepository.find({
where: { user: { id: getMyFarmApplyInput.user_id } },
relations: ['farm'],
order: {
application_num: 'DESC',
},
});
}
- like문
findFarmsWithLike({ getFarmsInput }: IFarmServiceGetFarms): Promise<Farm[]> {
return this.farmRepository.find({
where: {
farm_address: Like(`%${getFarmsInput.sido}%${getFarmsInput.sigungu}%`),
},
relations: ['user'],
});
}
- and 연산 : 객체 {}, or 연산: 배열 []
// and
checkFarmApply({ checkFarmInput }: IFarmServiceCheckFarm): Promise<Farm_Application> {
return this.farm_ApplicationRepository.findOne({
// and 연산 {}
where: {
farm: { farm_num: checkFarmInput.farm_num },
user: { id: checkFarmInput.user_id },
},
});
}
// or
findOneByInputInUser({ inputs }: IUsersServiceFindOneByInputInUser): Promise<User> {
return this.usersRepository.findOne({
// or 연산 []
where: [{ user_email: inputs.user_email }, { user_nick: inputs.user_nick }],
});
}
'코딩 > TypeORM' 카테고리의 다른 글
Find Options (0) | 2024.10.15 |
---|---|
Relationship (0) | 2024.10.15 |
[Entity] Table Inheritance - 클래스 상속을 이용한 테이블 늘리기 (1) | 2024.10.11 |
entities (0) | 2024.04.22 |
초기 설정 + 공식문서 링크 (0) | 2024.04.17 |