我有两个实体,分别名为user和Permission:
用户:
@ObjectType()
@Entity()
export class User {
@Field()
@PrimaryGeneratedColumn('uuid')
id: string;
@Authorized()
@Field()
@Column()
@Index({
unique: true,
})
email!: string;
@Field()
@Column()
name!: string;
@ManyToMany(
type => Permission,
permission => permission.users,
)
@JoinTable()
@Field(type => [Permission])
permissions: Permission[];
}
权限:
@ObjectType()
@Entity()
export class Permission {
@Field()
@PrimaryGeneratedColumn('uuid')
id: string;
@Field()
@Column()
@Index({
unique: true,
})
name!: string;
@ManyToMany(
type => User,
user => user.permissions,
)
@Field(type => [User])
users: User[];
}
现在我需要做的是查看电子邮件user@example.com
所标识的用户是否具有名称readData
所标识的权限。我尝试通过以下方式进行操作,但没有成功:
const userWithPermission = await userRepository
.createQueryBuilder('user')
.where('user.email = :email', { email: 'user@example.com' })
.leftJoinAndSelect('user.permissions', 'permission')
.where('permission.name = :name', { name: 'readData' })
.execute();
答案 0 :(得分:1)
尝试一下:
const userWithPermission = await userRepository
.createQueryBuilder("user")
.where("(user.email = :email) AND (permission.name = :name)", {
email: 'user@example.com',
name: 'readData'
})
.leftJoinAndSelect("user.permissions", "permission")
.execute();