Best way possible to get records with limit and offset along side with total records in Postgresql #610
-
Currently to filter records with limit-offset and get total count for it, i have to make 2 separate queries, one to count all records const [count, records] = await Promise.all([
ctx.db.select({ count: sql<number>`count(*)` }).from(tables).where(whereQueries),
ctx.db.select().from(tables).where(whereQueries).orderBy(orderQueries).limit(10).offset(10),
]) Which i think is not optimal at all, is there a way to get the results of the 2 queries above in 1 single query using drizzle? I found a solution but it is just raw SQL and i want to do this in drizzle way |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
i found a way by doing this below const data = await ctx.db.select({ record: table, count: sql<number>`count(*) over()` })
.from(table).where(...).limit(10).offset(0); But it make the shape of the result became: type results = {
record: Record;
count: number;
}[] That make each record item contain the total count, type results = {
records: Record[];
count: number;
} How can i do it? |
Beta Was this translation helpful? Give feedback.
i found a way by doing this below
But it make the shape of the result became:
That make each record item contain the total count,
which i want it to be separated like this:
How can i do it?