如何使用 TypeORM 中的 find-options API 在 where 对象中使用“OR”操作进行查询

Roh*_*pta 0 typeorm

我想在我的 repo 中找到结果,其中 firstname 类似于 'john' OR lastname 类似于 'doe' 但 findOptions where 子句将其视为 AND。

我试过的:

let results = await userRepo.find({
  where : {
    firstname : Like('%John%'),
    lastname : Like('%Doe%'),
  }
});
Run Code Online (Sandbox Code Playgroud)

我对此的期望:

let results = await userRepo.find({
  where : {
    firstname : Like('%John%'),
    lastname : Or(Like('%Doe%')),
  }
});
Run Code Online (Sandbox Code Playgroud)

关于如何在 where 对象中使用OR 的任何帮助?

noa*_*ner 7

可以使用简单的 typeorm .find() 选项来使用 OR 子句,如 TypeORM docs 中所述

要使用 OR 运算符进行查询,您需要使用条件数组而不是对象。

    let results = await userRepo.find({
        where: [
            { firstname: Like('%John%') },
            { lastname: Like('%Doe%') }
        ]
    });
Run Code Online (Sandbox Code Playgroud)