Code Monkey home page Code Monkey logo

typeorm-paginator's Introduction

TypeORM Paginator

Build Downloads Version License
dependencies Status devDependencies Status

It provides cursor-based pagination and page-based pagination. Even if there is a transformer in the column, it works perfectly.

Installation

npm install typeorm-paginator --save

Usage

Cursor-based Pagination

import { CursorPaginator } from 'typeorm-paginator'

Single cursor-based pagination.

const paginator = new CursorPaginator(User, {
  orderBy: {
    id: false,
  },
})

const pagination = await paginator.paginate(repoUsers.createQueryBuilder())

expect(pagination).toEqual({
  nodes: [
    /*
    User { id: 3 },
    User { id: 2 },
    User { id: 1 },
    */
  ],
  hasPrev: false,
  hasNext: false,
  nextCursor: expect.any(String),
  prevCursor: expect.any(String),
})

Multi cursor-based pagination.

const paginator = new CursorPaginator(User, {
  orderBy: [
    { name: true },
    { id: false },
  ],
})

const result = await paginator.paginate(repoUsers.createQueryBuilder(), { take: 2 })
expect(result).toEqual({
  nodes: [
    User { id: 3, name: 'a' },
    User { id: 5, name: 'b' },
  ],
  hasPrev: false,
  hasNext: true,
  prevCursor: expect.any(String),
  nextCursor: expect.any(String),
})

const resultNext = await paginator.paginate(repoUsers.createQueryBuilder(), { take: 2, nextCursor: result.nextCursor })
expect(resultNext).toEqual({
  nodes: [
    User { id: 2, name: 'b' },
    User { id: 6, name: 'c' },
  ],
  hasPrev: true,
  hasNext: true,
  prevCursor: expect.any(String),
  nextCursor: expect.any(String),
})

const resultNextNext = await paginator.paginate(repoUsers.createQueryBuilder(), { take: 2, nextCursor: resultNext.nextCursor })
expect(resultNextNext).toEqual({
  nodes: [
    User { id: 4, name: 'c' },
    User { id: 1, name: 'c' },
  ],
  hasPrev: true,
  hasNext: false,
  prevCursor: expect.any(String),
  nextCursor: expect.any(String),
})

const resultNextNextPrev = await paginator.paginate(repoUsers.createQueryBuilder(), { take: 2, prevCursor: resultNextNext.prevCursor })
expect(resultNextNextPrev).toEqual({
  nodes: [
    User { id: 2, name: 'b' },
    User { id: 6, name: 'c' },
  ],
  hasPrev: true,
  hasNext: true,
  prevCursor: expect.any(String),
  nextCursor: expect.any(String),
})

Page-based Pagination

import { PagePaginator } from 'typeorm-paginator'

Single cursor-based pagination.

const paginator = new PagePaginator(User, {
  orderBy: {
    id: false,
  },
  take: 3,
})

const pagination1 = await paginator.paginate(repoUsers.createQueryBuilder())

expect(pagination1).toEqual({
  nodes: [
    /*
    User { id: 5 },
    User { id: 4 },
    User { id: 3 },
    */
  ],
  hasNext: true,
})

const pagination1 = await paginator.paginate(repoUsers.createQueryBuilder(), { page: 2 })

expect(pagination1).toEqual({
  nodes: [
    /*
    User { id: 2 },
    User { id: 1 },
    */
  ],
  hasNext: false,
})

typeorm-paginator's People

Contributors

renovate-bot avatar wan2land avatar mohammadalikassem avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.