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-relay-cursor-pagination

v0.3.0

Published

[![Coverage Status](https://coveralls.io/repos/github/brietsparks/knex-relay-cursor-pagination/badge.svg?branch=coveralls)](https://coveralls.io/github/brietsparks/knex-relay-cursor-pagination?branch=coveralls)

Downloads

391

Readme

Knex Relay Cursor Pagination

Coverage Status

Easy Relay cursor pagination for your Knex queries

Install

yarn add knex-relay-cursor-pagination

Usage

import { createPagination } from 'knex-relay-cursor-pagination';

export type Post = {
  id: string;
  title: string;
  creation_timestamp: string;
};

async function getPosts(first?: number, after?: string, last?: number, before?: string): Promise<Page<Post>> {
  const pagination = createPagination({
    from: 'posts',
    sortColumn: 'creation_timestamp',
    sortDirection: 'desc',
    cursorColumn: 'id',
    first,
    after,
    last,
    before,
  });

  const rows = await this.db.from('posts')
    .where(pagination.where.column, pagination.where.comparator, pagination.where.value)
    .orderBy(pagination.orderBy.column, pagination.orderBy.direction)
    .limit(pagination.limit)
    .select('*');

  return pagination.getPage<Post>(rows);
}

Advanced sorting criteria

To sort by a derived sorting column, use Knex .with and pass the query alias to .from.

Here is an example of sorting posts by their count of associated comments:

const cte = db
    .from('posts')
    .select(
      'posts.*',
      db.raw(`concat(count("comments"."id"), ':', "posts".id) as comments_count`)
    )
    .leftJoin('comments', 'posts.id', 'comments.post_id')
    .groupBy('posts.id')
    .orderBy('comments_count', 'desc');

const pagination = createPagination({
  from: 'cte',
  sortColumn: 'comments_count',
  sortDirection: 'desc',
  cursorColumn: 'id',
  first,
  after,
  last,
  before,
});

const rows = await db
  .with('cte', cte)
  .from('cte')
  .where(
    pagination.where.column,
    pagination.where.comparator,
    pagination.where.value
  )
  .orderBy(pagination.orderBy.column, pagination.orderBy.direction)
  .limit(pagination.limit);

pagination.getPage<Post>(rows);

API

createPagination(params: PaginationParams)

Takes a configuration parameter and returns an object containing values to pass into the knex query and a function to transform the queried rows into a Relay connection page object.

Configuration parameters

Dataset configuration:

Required

  • cursorColumn: the column to be used for the cursor. Values must be unique
  • sortColumn: the column that the data is sorted by. Values must be unique
  • sortDirection: the sort direction of the data
  • from: the table or common-table-expression being queried from

Page slice configuration

Either first or last is required

  • first: corresponds to the first param of the Relay spec
  • after: corresponds to the after param of the Relay spec
  • last: corresponds to the last param of the Relay spec
  • before: corresponds to the before param of the Relay spec

Optional configuration

  • obfuscateCursor: a function for transforming the cursor's raw value to an opaque value
  • deobfuscateCursor: a function for transforming the cursor's opaque value to the raw value
  • onCursorMissing: 'omit' | 'throw', the behavior for when a queried row's cursor value is missing. Defaults to 'omit'

Examples

See the example app in the /example directory of this repo. See its README.md for more info.