Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
688 views
in Technique[技术] by (71.8m points)

typeorm - 如何从typeorm中的主要实体和关系中过滤掉?(How to filter from both the main entity and the relation in typeorm?)

I have two entities named user and permission like this:

(我有两个名为用户和许可的实体,如下所示:)

User:

(用户:)

@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[];
}

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[];
}

Now what I need to do is to see whether the user identified by the email [email protected] have the permission identified by the name readData .

(现在,我需要做的是查看电子邮件[email protected]所标识的用户是否具有名称readData标识的权限。)

I tried to do it in the following way but it did not work:

(我尝试通过以下方式进行操作,但没有成功:)

const userWithPermission = await userRepository
    .createQueryBuilder('user')
      .where('user.email = :email', { email: '[email protected]' })
      .leftJoinAndSelect('user.permissions', 'permission')
      .where('permission.name = :name', { name: 'readData' })
      .execute();
  ask by THpubs translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...