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

@tramwayjs/pager

v0.1.1

Published

A simple pager to handle pagination in the Tramway framework

Downloads

4

Readme

Tramway Pager is a simple pager to handle pagination in the Tramway framework. It includes:

  1. A Pager class
  2. A PaginatedCollection

Disclaimer: This library will include Paging-based parameters to include with queries sent via the Provider but it does not at this time. The Pager will only take the results returned as a Collection from the Repository via the Service and slice the part that needs to be returned with a request.

Installation:

  1. npm install @tramwayjs/pager

Pager

The Pager facilitates the process of taking a full collection and breaking it to a suitable PaginatedCollection to be used with the paginated formatter that is included in the tramway-formatter-hateaos library.

In order to use it, the corresponding Controller method that handles the collection you want to paginate needs to be overridden such to add the pager after the service has returned a collection. Some new query options should also be added to handle the dynamic input of page and limit values.

async get(req, res) {
    let items;
    const {page, limit, ...conditions} = req.query || {};

    try {
        items = await this.service.get(conditions);
    } catch (e) {
        this.logger.error(e.stack);
        return res.status(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    if (!items) {
        this.logger.info(`${this.constructor.name} - No items found`);
        return res.sendStatus(HttpStatus.BAD_REQUEST);
    }

    let pager = new Pager(items)
        .setPageSize(limit)
        .setCurrentPage(page)
    ;

    return this.sendCollection(res, pager.getItems(), {links: this.getLinks('get')});
}

Instead of passing the items directly to the sendCollection call, we create a new Pager, set the corresponding parameters and pass the PaginatedCollection returned by pager.getItems().

In the services configuration for the controller (found in src/config/services/controllers.js), replace the default HATEAOSFormatter with the paginated variant by changing the service declaration to {"type": "service", "key": "hateoas.service.formatter:paginated"}. This declaration will work if you have installed tramway-formatter-hateaos version 0.5.0 or newer.

The Pager exposes the following methods to be used.

| Function | Type | Usage | | --- | --- | --- | | setPageSize(pageSize: number) | instance | Sets the limit or pageSize to use when paging the collection | | setCurrentPage(page: number) | instance | Sets the current page number that is desired for the collection | | getTotal(): number | instance | The total number of pages required to get all the paginated content | | getItems(): PaginatedCollection | instance | A paginated collection with only the items for the specific page | | getPreviousPage(): number | instance | The page number of the previous page | | getNextPage(): number | instance | The page number of the next page | | hasNextPage(): boolean | instance | Whether there is a next page to iterate | | hasPreviousPage(): boolean | instance | Whether there is a previous page to iterate |

PaginatedCollection

The PaginatedCollection extends the base Collection class from traamway-core-connection and adds additional methods to facilitate pagination with the paginated formatter that is included in the tramway-formatter-hateaos library.

| Function | Type | Usage | | --- | --- | --- | | from(entities: Entity[]): PaginatedCollection | static | Creates a collection from an array of entities. let collection = PaginatedCollection.from(entities) | | getPage(): number | instance | The current page number of the collection | | getLimit(): number | instance | The total number of items that can be in the collection | | getPages(): number | instance | The total number of pages required to get all the paginated content | | getTotal(): number | instance | The total number of items that have been paginated |

To create an collection, extend the class.

import { PaginatedCollection } from '@tramwayjs/pager';