0.1.1 • Published 4 years ago

@tramwayjs/pager v0.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

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.

FunctionTypeUsage
setPageSize(pageSize: number)instanceSets the limit or pageSize to use when paging the collection
setCurrentPage(page: number)instanceSets the current page number that is desired for the collection
getTotal(): numberinstanceThe total number of pages required to get all the paginated content
getItems(): PaginatedCollectioninstanceA paginated collection with only the items for the specific page
getPreviousPage(): numberinstanceThe page number of the previous page
getNextPage(): numberinstanceThe page number of the next page
hasNextPage(): booleaninstanceWhether there is a next page to iterate
hasPreviousPage(): booleaninstanceWhether 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.

FunctionTypeUsage
from(entities: Entity[]): PaginatedCollectionstaticCreates a collection from an array of entities. let collection = PaginatedCollection.from(entities)
getPage(): numberinstanceThe current page number of the collection
getLimit(): numberinstanceThe total number of items that can be in the collection
getPages(): numberinstanceThe total number of pages required to get all the paginated content
getTotal(): numberinstanceThe total number of items that have been paginated

To create an collection, extend the class.

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

4 years ago

0.1.0

4 years ago