Skip to content

Best way possible to get records with limit and offset along side with total records in Postgresql #610

Discussion options

You must be logged in to vote

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,
which i want it to be separated like this:

type results = {
  records: Record[];
  count: number;
}

How can i do it?

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Angelelz
Comment options

Answer selected by Senbonzakura1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants