在我的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实现这一点有什么想法吗?
发布于 2018-08-31 16:09:39
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
https://stackoverflow.com/questions/52100765
复制相似问题