Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
knex-relay-cursor-pagination
Advanced tools
[![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)
Readme
Easy Relay cursor pagination for your Knex queries
yarn add knex-relay-cursor-pagination
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);
}
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);
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.
Required
cursorColumn
: the column to be used for the cursor. Values must be uniquesortColumn
: the column that the data is sorted by. Values must be uniquesortDirection
: the sort direction of the datafrom
: the table or common-table-expression being queried fromEither first
or last
is required
first
: corresponds to the first
param of the Relay specafter
: corresponds to the after
param of the Relay speclast
: corresponds to the last
param of the Relay specbefore
: corresponds to the before
param of the Relay specobfuscateCursor
: a function for transforming the cursor's raw value to an opaque valuedeobfuscateCursor
: a function for transforming the cursor's opaque value to the raw valueonCursorMissing
: 'omit' | 'throw'
, the behavior for when a queried row's cursor value is missing. Defaults to 'omit'
See the example app in the /example directory of this repo. See its README.md for more info.
FAQs
[![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)
The npm package knex-relay-cursor-pagination receives a total of 102 weekly downloads. As such, knex-relay-cursor-pagination popularity was classified as not popular.
We found that knex-relay-cursor-pagination demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.