Typeorm:检查属性是否为值 1、2、3 或 n

san*_*oco 2 filtering querying repository typeorm

我想获得所有具有特定role. 我有一个字符串数组,我想为其获取任务。

询问:

return this.createQueryBuilder('task')
    .select(this.baseSelect)
    .where('task.role = :role', { role }) // What here?
    .getMany();
Run Code Online (Sandbox Code Playgroud)

当然,这段代码仅获取角色为该值的任务。如何检查多个值?

Jud*_*dge 6

要以多个角色进行搜索,您可以使用 IN 运算符:

TypeORM 的旧版本

return this.createQueryBuilder('task')
    .select(this.baseSelect)
    .where('task.role IN(:roles)', {roles: [role1, role2, role3]});
    .getMany();
Run Code Online (Sandbox Code Playgroud)

新版本的 TypeORM

正如用户 Christophe Geers 所提到的,在新版本中,您将必须使用扩展语法。

return this.createQueryBuilder('task')
    .select(this.baseSelect)
    .where('task.role IN(:...roles)', {roles: [role1, role2, role3]});
    .getMany();
Run Code Online (Sandbox Code Playgroud)

  • 在较新的版本中,您必须使用扩展语法。...where('task.role IN(:...roles)', { 角色: [角色1, 角色2, 角色3]}); 参考:https://github.com/typeorm/typeorm/issues/1239 (2认同)