Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Seek Pagination with Knex library for Postgress

Attempting to implement cursor based pagination - and I would like to know the proper way to call it with Knex for Postgres.

Here is the standard statement:

SELECT 
FROM 
WHERE (timestamp, id) > (cursor.age, cursor.id)
ORDER BY timestamp ASC, id ASC
LIMIT

And my attempt with Knex:

const knexResult = await knex({ 'table_name' })
    .select(columns)
    .where('created_at', '>', cursor.timestamp)
    .andWhere('id', '>', cursor.id)
    .orderBy(['timestamp', 'id'])
    .limit(first)
  return knexResult;

Is this the proper way to call it - it seems not correct... I am trying to avoid knex.raw

Edit

The SQL standard for the statement (x,y) > (a,b) is true if:

                          (x > a or (x = a and y > b))

The code above gives no-results because I believe its trying to match both where clauses.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can implement this boolean condition (x > a or (x = a and y > b)) with knex.

const knexResult = await knex({ 'table_name' })
    .select(columns)
    .where('created_at', '>', cursor.timestamp)
    .orWhere((inner) => 
       inner.where('created_at', '=', cursor.timestamp)
            .andWhere('id', '>', cursor.id)
    )
    .orderBy(['timestamp', 'id'])
    .limit(first);

  return knexResult;

It will produce:

Select `col1`, `col2` from `table_name` where `created_at` > '12323112' or (`created_at` = '12323112' and id > '23')
Order by `timestamp`, `id`
Limit 1
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!