Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda