@Entity({ name: 'user_cell_transfer' })
export class UserCellTransferEntity extends DefaultEntity {
@ManyToOne(() => UserEntity, { nullable: false })
@JoinColumn({ name: 'user_id' })
user: UserEntity;
@ManyToOne(() => CellEntity, { nullable: false })
@JoinColumn({ name: 'from_cell_id' })
fromCell: CellEntity;
@ManyToOne(() => CellEntity, { nullable: false })
@JoinColumn({ name: 'to_cell_id' })
toCell: CellEntity;
@Column('varchar')
status: UserCellTransferStatus;
@Column({ type: 'date', comment: '신청일자' })
orderDate: string;
@Column({ type: 'date', comment: '신청처리 완료일자', nullable: true })
completeDate: string | null;
}
위와 같이 UserCellTransferEntity 내에 연관관계가 있는 user, fromCell, toCell 프로퍼티가 있는데, 해당 프로퍼티를 연관객체가 아닌 key 만 조회해오고 싶었습니다.
await entityManager.getRepository(UserCellTransferEntity).find({
where: {
toCell: {
id: cellId,
},
},
relations: ['user', 'fromCell', 'toCell'],
});
위와 같이 조회하면, user, fromCell, toCell Entity에 대한 모든 프로퍼티를 조회하기 때문에 SQL 문도 길어지고 조회해오는 컬럼 자체도 많아져서 성능상 좋지 않습니다.
await entityManager.getRepository(UserCellTransferEntity).find({
where: {
toCell: {
id: cellId,
},
},
// relations: ['user', 'fromCell', 'toCell'],
loadRelationIds: {
relations: ['user', 'fromCell', 'toCell'],
},
});
옵션을 찾던 중, 위와 같이 관계 id 만 로드하는 옵션이 있어서 쿼리를 해봤는데 아래 스크린샷과 같이 객체의 형태가 보존되지 않아서 뭔가 자연스럽지 않았습니다. (일단, type safe 하지 않았습니다...)
await entityManager.getRepository(UserCellTransferEntity).find({
where: {
toCell: {
id: cellId,
},
},
loadRelationIds: {
relations: ['user', 'fromCell', 'toCell'],
disableMixedMap: true,
},
});
loadRelationIds 옵션중 disableMixedMap 이라는 옵션이 있어서 해당 옵션을 true 로 설정했더니, 아래와 같이 원하는 결과가 나왔습니다.