【发布时间】:2021-10-15 11:46:03
【问题描述】:
谁能帮我知道如何在 typeorm 中填充查询。就像我有这个实体
@Entity('users')
export class User extends BaseEntity {
@Column()
userName : string;
@Column()
email : string;
@OneToMany(() => UserFollowing, (userFollowing) => userFollowing.followee)
followers: User[];
@OneToMany(() => UserFollowing, (userFollowing) => userFollowing.follower)
followees: User[];
}
@Entity('user_followings')
export class UserFollowing extends FakeBaseEntity {
@JoinColumn({ name : 'follower_id' })
@ManyToOne(() => User, user => user.followees)
follower : User;
@JoinColumn({ name : 'followee_id' })
@ManyToOne(() => User, user => user.followers)
followee : User;
}
现在获取特定userid 的所有followers 和followees
这是我的两种方法:都给出相同的输出
const info = await this.userRepo
.createQueryBuilder('userFollowing')
.select()
.leftJoinAndSelect('userFollowing.followers','followers')
.leftJoinAndSelect('userFollowing.followees', 'followees')
.where('userFollowing.id = :userid', { userid })
.getMany()
return info;
------------------------------------------------------------
const info = this.userRepo.find({
where: {
id : userid,
},
relations: ["followers", "followees"],
})
return info;
我收到的输出:我想要关于关注者和关注者的所有信息
{
"id": "e8651d4f-3c7b-4f5a-8205-7370b107d98c",
"userName": "something",
"email" : "something@gmail.com",
"followers": [
{
"id": "f54b8574-10ea-4133-85bd-5f8fcda4eeb9",
"createdAt": "2021-08-12T03:58:39.198Z",
"updatedAt": "2021-08-12T03:58:39.198Z"
}
],
"followees": [
{
"id": "eb2cb728-a1c0-4bea-9230-712827c714c7",
"createdAt": "2021-08-12T03:59:32.260Z",
"updatedAt": "2021-08-12T03:59:32.260Z"
}
]
}
【问题讨论】:
标签: sql typescript nestjs typeorm