Is there any easy way to query both total count and pagination result?

The official doc shows a simple pagination query with dynamic query:
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
) {
return qb.limit(pageSize).offset((page - 1) * pageSize);
}
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
) {
return qb.limit(pageSize).offset((page - 1) * pageSize);
}
But how to also return the total count ?
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
): {count: number, result: any}
function withPagination<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
): {count: number, result: any}
I know Grails GORM has a PagedResultList wrapper to do so : https://docs.grails.org/latest/ref/Domain%20Classes/createCriteria.html When pass pagination query params it will query database twice, one is total count query, and another is the pagination result, and wrapper them into a PagedResultList class. Is there any way to do this in Drizzle ORM? Like:
function withPaginationAndCount<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
): {count: number, result: any} {
// how to rewrite like this?
const count = qb.select({count: count()})...;
count paginationResult = qb.limit(pageSize).offset((page - 1) * pageSize);
return Promise.all([count, paginationResult]);
}
function withPaginationAndCount<T extends PgSelect>(
qb: T,
page: number = 1,
pageSize: number = 10,
): {count: number, result: any} {
// how to rewrite like this?
const count = qb.select({count: count()})...;
count paginationResult = qb.limit(pageSize).offset((page - 1) * pageSize);
return Promise.all([count, paginationResult]);
}
Thanks.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?