Instead of running two separate queries, is there a way to retrieve the total count and records together in a single query?
If combining them is not possible, is there a method to reuse the where condition in both queries?
async findAll(query): Promise<Paginate> {
const take = query.take || 10
const skip = query.skip || 0
const keyword = query.keyword || ''
const builder = this.userRepository.createQueryBuilder("user")
const total = await builder.where("user.name like :name", { name: '%' + keyword + '%' }).getCount()
const data = await builder.where("user.name like :name", { name: '%' + keyword + '%' }).orderBy('name', 'DESC').skip(skip).take(take).getMany();
return {
data: data,
count: total
}
}
{
count: 10,
data: [
{
id: 1,
name: 'David'
},
{
id: 2,
name: 'Alex'
}]
}