提问者:小点点

类似TypeORM QueryBuilder中带通配符的查询


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

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

现在我只想返回名称中包含john的用户。在SQL中,这将是一个类似查询类似%john%

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

如何执行like查询类型ORM给出了一个解决方案:。Where(“user.firstName like:name”,{name:'%'+firstName+'%'})

但是,我无法使用skiptake,后者在使用where()而不是find()时可用。

对于如何使用TypeORM QueryBuilder实现这一点有什么想法吗?


共1个答案

匿名用户

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();

有关详细信息,请参见文档:在TypeORM QueryBuilder中使用分页