在我的NestJS项目中,我有这个TypeORM查询:
const users = await this.usersRepository.find({
skip,
take,
order: sortingObject,
join: {
alias: 'user',
leftJoinAndSelect: {
country: 'user.country_id',
},
},
});
现在我只想用名字中的John
返回用户。在SQL中,这将是一个LIKE
查询LIKE %John%
。
在https://github.com/typeorm/typeorm/blob/master/docs/find-options.md,没有关于通配符LIKE
查询的信息。
How to perform a like query Typeorm给出了一个解决方案:
.where("user.firstName like :name", {name: '%' + firstName + '%' })
但是当我使用skip
而不是take
时,我无法使用where()
和find()
。
有关如何使用TypeORM QueryBuilder实现此目的的任何想法?
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();
有关详细信息,请参阅文档:Using Pagination in TypeORM QueryBuilder