Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
714 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...