Relation Options
// find() 실행 할 때마다 항상 같이 가져올 relation -> eager
eager: true,
// 저장 할 때 realation을 한 번에 같이 저장
cascade: true,
// nullable
nullable: true,
// on : ~ 했을 때 -> on delete 삭제 했을 때
// 관계가 삭제되었을 때
// no action -> 아무것도 안 함
// cascade -> 참조하는 Row도 같이 삭제
// set null -> 참조하는 Row에서 참조 id를 null로 변경
// set default -> 기본 세팅으로 설정 (테이블의 기본 세팅)
// restrict -> 참조하고 있는 Row가 있는 경우 참조 당하는 Row 삭제 불가
onDelete: 'RESTRICT',
FindManyOptions
// async getUsers() {
// const users = await this.userRepository.find({
// relations: {
// profile: true,
// posts: true,
// },
// });
// return users;
// }
// 어떤 프로퍼티를 선택할지
// 기본은 모든 프로퍼티를 가져온다
// 만약에 select를 정의하지 않으면
// select를 정의하면 정의된 프로퍼티만 가져오게 된다.
// select: {}
// where : 필터링할 조건을 입력하게 된다.
// where : {version : 1,}
// 관계를 가져오는 법
// relations: {}
// 오름차 내림차
// ASC DESC
// order: { id: 'DESC', }
// skip : 처음 몇 개를 제외할지
// skip : 2
// take : 몇 개를 가져올지
// take : 2
TypeORM 유틸리티
this.userRepository.find({
order: {
id: 'asc',
},
where: {
아닌 경우 가져오기
id: Not(1),
적은 경우 가져오기
id: LessThan(30),
적은경우 or 많은 경우
id : LessThanOrEqual(30)
많은 경우
id : MoreThan(30),
많거나 같은 경우
id : MoreThanOrEqual(30),
같은 경우
id: Equal(30),
유사값
email: Like('%google%'),
근데 대문자는 필터링 불가해서 ILike 사용
email: ILike('%google%'),
사이값
id: Between(10, 15),
해당되는 여러개의 값
id: In([1, 3, 5, 7, 9]),
자주 사용되는 메서드
@Post('sample')
async sample() {
// 모델에 해당되는 객체 생성 - 저장은 안 합니다! create
// const user1 = this.userRepository.create({
// email: 'test@gmail.com',
// });
// 객체 생성하고 저장 하는 save
// const user2 = this.userRepository.save({
// email: 'test2@gmail.com',
// })
// preload
// 입력된 값을 기반으로 데이터베이스에 있는 데이터를 불러오고
// 추가 입력된 값으로 데이터베이스에서 가져온 값들을 대체함
// 저장하지는 않음
// const user3 = await this.userRepository.preload({
// id: 101,
// email: 'test3@gmail.com',
// });
// 삭제하기
// await this.userRepository.delete(
// 101,
// );
// return true;
// id가 1인 항목의 count를 2만큼 증가시킨다. increment
// id가 1인 항목의 count를 2만큼 감소시킨다. decrement
// await this.userRepository.increment({
// id: 1,
// }, 'count', 2);
// 갯수 카운팅하기
// const count = await this.userRepository.count({
// where: {
// email: ILike('%0%'),
// },
// })
// sum
// const sum = await this.userRepository.sum('count', {
// email: ILike('%0%'),
// });
// average
// const average = await this.userRepository.average('count', {
// email: ILike('%0%'),
// });
// 최소값
// const min = await this.userRepository.minimum('count', {
// id: LessThan(4),
// });
// 최대값
// const max = await this.userRepository.maximum('count', {
// id: LessThan(4),
// })
const usersAndCount = await this.userRepository.findAndCount({
take: 3,
})
// list and 필터링 하지 않았으면 될 값 return 해준다
return usersAndCount;
}