npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knex-cursor-paginator

v1.0.0

Published

Cursor based pagination for a [knex](https://www.npmjs.com/package/knex) query.

Downloads

3

Readme

knex-paginate

Cursor based pagination for a knex query.

Installation

npm i knex-cursor-paginator

Usage

import { Paginator } from "knex-cursor-paginator";
import { knex } from "knex";

const database = knex({/*connection config*/});

const query = database("users").select("*");
const paginator = new Paginator(query, {/*paginate config*/});

const firstPage = await paginator.next();
const secondPage = await paginator.next();
const firstPageAgain = await paginator.previous();

Paginate Config

| Name | Type | Description | |----------------|:-----|:-------------| | cursorColumn | string | Used as the cursor, must be unique.| | orderByColumn| string | (Optional) Primary sort key. | | order | asc\|desc | The sort order of the results. Results are ordered by (orderByColumn, cursorColumn). If orderByColumn is not provided then only cursorColumn is used| |pageSize | number | Number of results to be returned in each page. Equivalent to limit pageSize|

Important

The cursorColumn and orderByColumn column need to correspond to a field in the select statement, for example:

import { PaginateConfig } from "knex-cursor-paginator";

const query = database("users").select("id", "name")

const config: PaginateConfig = {
    cursorColumn: "id",
    orderByColumn: "name",
    order: "asc",
    pageSize: 10
}

Both the cursorColumn and orderByColumn both appear in the select statement of the query. See the below examples to deal with aliases and computed columns.

Examples

Generic

Will sort based on (age asc, id asc). Returning an array of length 2 with each fetch.

// users = [
//     { id: 0, name: "a" },
//     { id: 1, name: "b" },
//     { id: 2, name: "c" },
//     { id: 3, name: "c" },
// ];
const query = database("users").select("*")

const config: PaginateConfig = {
    cursorColumn: "id",
    orderByColumn: "age",
    order: "asc",
    pageSize: 2
}

// [{ id: 0, name: "a" }, { id: 1, name: "b" }]
const firstPage = await paginator.next();

// [{ id: 2, name: "c" }, { id: 3, name: "c" }]
const secondPage = await paginator.next();

With table alias

const query = database("users as u").select("u.id", "u.name").leftJoin("foo as f", "f.id", "u.id")

const config: PaginateConfig = {
    cursorColumn: "u.id",
    orderByColumn: "u.age",
    order: "asc",
    pageSize: 10
}

With column alias

const query = database("users").select("id", "CONCAT(firstName, lastName) as name")

const config: PaginateConfig = {
    cursorColumn: "id",
    orderByColumn: "CONCAT(firstName, lastName) as name",
    order: "asc",
    pageSize: 10
}

Skipping pages

const paginator = new Paginator(query, {/*paginate config*/});

const firstPage = await paginator.next();
const thirdPage = await paginator.next({ pageOffset: 1 });

Saving state

If you need to save the paginator's state across api calls, you can serialize the object for later use.

const query = database("users").select("*")
const paginator = new Paginator(query, {/*paginate config*/});

const firstPage = await paginator.next();

const json = JSON.stringify(paginator);
const connection = uuid();
await cache.set(connection, json);

return firstPage

// -> call to fetch next page

const json = await cache.get(connection)
const paginator = new Paginator(query, json)
const secondPage = await paginator.next();