TypeORM QueryBuilder 中带通配符的 LIKE 查询

Bas*_*ijk 2 typeorm nestjs typeorm-datamapper

在我的 NestJS 项目中,我有这个 TypeORM 查询:

const users = await this.usersRepository.find({
  skip,
  take,
  order: sortingObject,
  join: {
      alias: 'user',
      leftJoinAndSelect: {
          country: 'user.country_id',
      },
  },
});
Run Code Online (Sandbox Code Playgroud)

现在我只想返回John名称中的用户。在 SQL 中,这将是一个LIKE查询LIKE %John%

https://github.com/typeorm/typeorm/blob/master/docs/find-options.md中没有关于通配符的信息LIKE查询的。

如何执行类似查询 Typeorm作为解决方案提供: .where("user.firstName like :name", {name: '%' + firstName + '%' })

但是后来我无法使用skiptake并且在使用时可用where()代替find().

关于如何使用 TypeORM QueryBuilder 实现这一点的任何想法?

Mic*_*Lam 6

QueryBuilder 中有分页方法(.skip(int).take(int))。

尝试这样的事情。

const users = await this.usersRepository
    .createQueryBuilder("user")
    .leftJoinAndSelect("user.country_id", "country")
    .skip(5)
    .take(10)
    .where("user.firstName like :name", {name: '%' + firstName + '%' })
    .orderBy("user.id", "DESC")
    .getMany();
Run Code Online (Sandbox Code Playgroud)

具体请参考文档: Using Pagination in TypeORM QueryBuilder

  • 我的问题不是如何使用skip/take方法,而是如何将它们与通配符LIKE查询结合使用 (2认同)