Skip to content

Easy and simple pagination plugin to build pagination objects with typeorm

License

Notifications You must be signed in to change notification settings

JBurgosDev/typeorm-easy-paginate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub license GitHub stars GitHub issues GitHub package.json version

typeorm-easy-paginate

The fast and easy way to create paging objects in Express with TypeORM

Installation

Pre-requisites:

  • typeorm

NPM:

npm install typeorm-easy-paginate
Yarn
yarn add typeorm-easy-paginate

Usage

Import the plugin where you want to use it

import { paginate } from "typeorm-easy-paginate";

Now we can start paging our queries.

We create our controller and call the function paginate

import { Response, Request } from 'express';
import { Example } from "../entity/example";
import { paginate } from "typeorm-easy-paginate";

// Example controller
export class exampleController {
    public async example(req: Request, res: Response) {
        
        /* Replace 'Example' with your entity */
        const paginateList = await paginate<Example>(Example, { /*Paginate options here*/ }, {/*TypeOrm repository querys here*/});

        return res.json(paginateList)
    }
}

In the paging options you can pass the page, and the number of records per page either manually or from query params.

NOTE:

By default it will always start from page 1.

If you do not pass the number of records per page, the total number of records in the table will be taken by default

const paginateList = await paginate<Example>(Example, {page: 1, perPage: 10}, {});

You can pass options from query params. You must convert it to numeric like this

let page = parseInt(<string>req.params.page);
let perPage = parseInt(<string>req.params.perPage);

And now you can pass to the options

const paginateList = await paginate<Example>(Example, {page, perPage}, {});

Query

In addition to the paging options, you can make queries like these.

PD: You can use all FindOptions of TypeORM

const paginateList = await paginate<Example>(Example, {page, perPage}, {where: { firstName: "Timber" }});

Request:

You can make the url like this

http://localhost:3000/api/jobs?page=1&perPage=15

Or

http://localhost:3000/api/jobs

Example result:

{
  "content": [
    {
      "id": 13,
      "job": "001"
    },
    {
      "id": 14,
      "job": "002"
    }
  ],
  "pagination": {
    "page": 1,
    "perPage": 2,
    "totalPage": 30,
    "totalItems": 60
  }
}

Contributions:

If you like to make a contribution you can send a PR, I will be very grateful for your help. PULL REQUESTS

About

Easy and simple pagination plugin to build pagination objects with typeorm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published