Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda