sequelize-typescript-paginator
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

Sequelize Typescript Paginator

A utility to paginate a model using sequelize-typescript package by RobinBuschmann.

Installation

npm install sequelize-typescript-paginator

Usage

Simplest way is to call paginate and pass the Model class to the 1st parameter. By default it will paginate 10 items without any filter, and ordering.

import { PaginationResponse, SequelizePaginator } from "sequelize-typescript-paginator";

async fetch(): PaginationResponse {
    return await SequelizePaginator.paginate(UserModel);
}

Code above will return an object with data and meta inside of it.

export class PaginationMeta {
  perPage: number;
  page: number;
  totalPage?: number;
  total?: number;
}

export class PaginationResponse<T> {
  data: Array<T>;
  meta: PaginationMeta;
}

For advance use, you can simply pass PaginationMeta in second paramter of paginate and FindOptions in third parameter as shown below.

import { PaginationResponse, SequelizePaginator } from "sequelize-typescript-paginator";

async fetch(): PaginationResponse {
    return await SequelizePaginator.paginate(
        UserModel,
        {
            perPage: 10,
            page: 1,
        },
        {
            where: { email_verified_at: { [Op.ne]: null } },
            order: [['created_at', 'desc']],
        },
    );
}

Todo List

  • [ ] Add test
  • [ ] Use 2 parameters instead of 3
  • [ ] Configurable default perPage value

Package Sidebar

Install

npm i sequelize-typescript-paginator

Weekly Downloads

1

Version

0.2.0

License

ISC

Unpacked Size

256 kB

Total Files

8

Last publish

Collaborators

  • halimtuhu