如何使用 TypeORM 获取按关系排序的数据?
How to get data sorted with relation using TypeORM?
我想获得具有排序关系的用户数据,但使用此代码它只是对用户进行排序,但我想对与我正在使用的用户有关系的数据进行排序,有人可以帮助我吗?
getUerWithId(pramas: string): Observable<userEntity[]> {
return from(this.userRepository.find({
where: [
{ id: pramas }
],
order:{
id:'DESC'
}
}))
}
使用存储库,我不知道是否可行,但您可以尝试使用查询生成器来获取有序关系。遵循以下示例:
this.userRepository.createQueryBuilder()
.innerJoinAndSelect("User.notification", "Notification")
.orderBy({'Notification.createdAt': 'DESC'})
.where("User.id = :id", {
id: Number(id),
})
.getOne();
记得为您的实体设置正确的关系,并为您的属性设置正确的名称。
我想获得具有排序关系的用户数据,但使用此代码它只是对用户进行排序,但我想对与我正在使用的用户有关系的数据进行排序,有人可以帮助我吗?
getUerWithId(pramas: string): Observable<userEntity[]> {
return from(this.userRepository.find({
where: [
{ id: pramas }
],
order:{
id:'DESC'
}
}))
}
使用存储库,我不知道是否可行,但您可以尝试使用查询生成器来获取有序关系。遵循以下示例:
this.userRepository.createQueryBuilder()
.innerJoinAndSelect("User.notification", "Notification")
.orderBy({'Notification.createdAt': 'DESC'})
.where("User.id = :id", {
id: Number(id),
})
.getOne();
记得为您的实体设置正确的关系,并为您的属性设置正确的名称。