在我的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+'%'})
但是,我无法使用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();
有关详细信息,请参见文档:在TypeORM QueryBuilder中使用分页