在 Typeorm 中填充查询
Populaing a query in Typeorm
谁能帮我知道如何在 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"
}
]
}
如果我没有正确理解你的问题,那么你正在寻找的是获取关注者和被关注者的数据。
您可以使用 find
函数轻松实现此目的。
您的查询应该是这样的:
const info = this.userRepo.find({
where: {
id : userid,
},
relations: ["followers", "followees", "followers.follower", "followees.followee"],
})
如您所见,我在 relations
中又传递了两个字符串值。
使用它,它也会加载子关系(在 TypeORM Find Options 中说明)。
您也可以使用查询构建器通过添加更多 .leftJoinAndSelect
方法链来实现相同的目的。
外部链接:
谁能帮我知道如何在 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"
}
]
}
如果我没有正确理解你的问题,那么你正在寻找的是获取关注者和被关注者的数据。
您可以使用 find
函数轻松实现此目的。
您的查询应该是这样的:
const info = this.userRepo.find({
where: {
id : userid,
},
relations: ["followers", "followees", "followers.follower", "followees.followee"],
})
如您所见,我在 relations
中又传递了两个字符串值。
使用它,它也会加载子关系(在 TypeORM Find Options 中说明)。
您也可以使用查询构建器通过添加更多 .leftJoinAndSelect
方法链来实现相同的目的。
外部链接: