one to one, one to Many, Many to One 모두다 똑같다.find() 실행 할때마다 항상 같이 가져올 relation
// userEntity.ts
@OneToOne(() => ProfileModel, (profile) => profile.user, {
eager: true,
})
profile: ProfileModel;
@Get('users')
getUsers() {
return this.userRepository.find({
// 이렇게 relations 안 걸어도됨.
relations: {
profile: true,
},
});
}
get user할때 find메서드의 옵션인 relation을 걸어놓지 않아도, profile까지 가져온다.
eager : true : user 데이터에 profile 데이터가 자동으로 포함됨.

eager : false: user 데이터에 profile 데이터가 포함되지 않음.

cascade 옵션 (기본값은 false)
저장할때 relation을 한번에 같이 저장하는 기능 true 했을때
relation 에 해당되는 다른 테이블 데이터를 한번에 저장할 수 있다.
export class UserModel {
@OneToOne(() => ProfileModel, (profile) => profile.user, {
eager: true,
cascade : true
})
}
@Post('user/profile')
async createUserAndProfile() {
const user = await this.userRepository.save({
email: '[email protected]',
profile : {
profileImg: 'kks.jpg',
}
});
// 따로 저장하는 방법
// const profile = await this.profileRepository.save({
// profileImage: 'kks.jpg',
// user,
// });
return profile;
}
nullable 옵션 기본값은 true
realation은 null이 가능하다.nullable을 false로 설정하면, 무조건 값을 넣어야한다.onDelete 옵션
CASCADE : 참조하는 Row도 같이 삭제
delete 요청으로 삭제하면, 참조하고 있는 user도 같이 삭제가 된다.SET NULL: 참조하는 Row에서 참조 id를 null로 변경
delete 요청으로 삭제하면, 참조하고 있는 user 데이터의 profile 값이 null이 된다.