knex-cursor-pagination
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Knex Cursor Pagination

Add stable cursor pagination to Knex query builder. This package also supports relay connection result.

Installation

yarn add knex-cursor-pagination

Usage

import { knexCursorPagination, getCursor } from 'knex-cursor-pagination'

let query = knex
  .queryBuilder()
  .select('users.*')
  .from('users')
  .orderBy('posts.created_at', 'DESC')

query = knexCursorPagination(query, { after: 'your-cursor' })

const results = await query;
const endCursor = getCursor(results[results.length - 1]);

Relay Pagination

Use with Relay pagination for example in Apollo Server

import { relayConnection } from 'knex-cursor-pagination'

const args = {
  first: 10,
  after: 'your-cursor',
}

const query = knex
  .queryBuilder()
  .select('users.*')
  .from('users')
  .orderBy('posts.created_at', 'DESC')

const result = relayConnection({
  query,
  args,
})

Known issues

  • Currently not fully working with backwards pagination
  • Using orderByRaw you need explicity to provide ASC or DESC

License

The package is available as open source under the terms of the MIT License.

/knex-cursor-pagination/

    Package Sidebar

    Install

    npm i knex-cursor-pagination

    Weekly Downloads

    6

    Version

    0.3.0

    License

    MIT

    Unpacked Size

    76.5 kB

    Total Files

    25

    Last publish

    Collaborators

    • jpalumickas