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

aethon-nestjs-paginate

v0.0.4

Published

A simple pagination add-in for Nest.js

Downloads

285

Readme

aethon-nestjs-paginate

Description

(Yet another) NestJS pagination add-in, composed of a decorator and associated classes. Inspired by nestjs-paginate, but with narrower features and simpler code.

Also allows pagination of arbitrary data held in an array or any Promise returning an array, not just from a database. This is useful when returning paginated data from a cache. We use this functionality in applications with significant client-side analytics that require low latency over large datasets.

Pagination schema

The pagination schema is described in the dependency package aethon-paginate-types.

Example usage

Installation

npm install -s aethon-nestjs-paginate

Configuring pagination

export class PaginateConfig {
    limit: number;                  // default pagination limit
    limitMax: number;               // maximum limit allowed, to throttle request
    orderBy?: OrderBy;              // default ordering of the results
    relations?: RelationshipParam[] // array defining the INNER JOINS to be performed
}

type RelationshipParam {
    joinProperty: string;           // the TypeORM join property e.g. user.group
    entityName: string;             // the TypeORM table name to join with e.g. group
}

Intercepting a request with @GetPaginator

In the required NestJS controller endpoint, use the @GetPaginator(config: PaginateConfig) decorator to return an instance of the Paginator class that will hold the pagination query request parameters and anticipate a dataset to perform the requested query over, as follows:

async index(
        @GetPaginator(testDataConfig) paginator: Paginator): Promise<Paginated<TestEntity>> {
        return this.testService.findAll(paginator);
}

Request format

The inbound request must conform to the PaginateQuery interface in aethon-paginate-types.

export interface PaginateQuery {
    page?: number;
    limit?: number;
    orderBy?: OrderBy;      // [[string, "ASC" | "DESC"]]
    where?: Where;          // [[string, string, string]], with the first string is a field name, the second a comparison operator and the third one the value it should be equal to
}

Paginating query results

If paginating entities from a TypeORM-connected database, pass the relevant TypeORM repository to the Paginator.run() method in your corresponding NestJS service:

async findAll(paginator: Paginator): Promise<Paginated<TestEntity>> {
        const source = this.dataSource.getRepository(TestEntity);
        return paginator.run(source);
    }

For any generic type T (in this case, TestEntity), source in the above example can be any of TypeORM Repository<T>, an array T[] or a Promise returning an array Promise<T[]>.

Dependencies/ extensions included

  1. TypeORM

Features set up

  • Where queries, equality only (e.g. WHERE groupId=2) with multiple AND clauses
  • Nested OrderBy queries
  • JOINs

To do

  • Where queries for greater than, greater or equal than etc.
  • OR clauses in Where