GithubHelp home page GithubHelp logo

mrjamesjcho / typeorm-cursor-pagination Goto Github PK

View Code? Open in Web Editor NEW

This project forked from benjamin658/typeorm-cursor-pagination

0.0 0.0 0.0 294 KB

Cursor-based pagination works with TypeORM Query Builder.

License: MIT License

TypeScript 100.00%

typeorm-cursor-pagination's Introduction

TypeORM Cursor Pagination

Build Status codecov npm version Maintainability license

Cursor-based pagination works with TypeORM Query Builder.

Why or What is Cursor-Based Pagination

If this project is helpful to you, I truly appreciate you all for your stars ⭐⭐⭐ and contributions 💪💪💪.

Installation

npm install typeorm-cursor-pagination --save

Usage

Query first page without any cursor

import { getConnection } from "typeorm";
import { buildPaginator } from 'typeorm-cursor-pagination';

const queryBuilder = getConnection()
  .getRepository(User)
  .createQueryBuilder('user')
  .where("user.gender = :gender", { gender: 'male' });

const paginator = buildPaginator({
  entity: User,
  paginationKeys: ['id'],
  query: {
    limit: 10,
    order: 'ASC',
  },
});

// Pass queryBuilder as parameter to get paginate result.
const { data, cursor } = await paginator.paginate(queryBuilder);

The buildPaginator function has the following options

  • entity [required]: TypeORM entity.
  • alias [optional]: alias of the query builder.
  • paginationKeys [optional]: array of the fields to be used for the pagination, default is id.
  • query [optional]:
    • limit: limit the number of records returned, default is 100.
    • order: ASC or DESC, default is DESC.
    • beforeCursor: the before cursor.
    • afterCursor: the after cursor.

paginator.paginate(queryBuilder) returns the entities and cursor for the next iteration

interface PagingResult<Entity> {
  data: Entity[];
  cursor: Cursor;
}

interface Cursor {
  beforeCursor: string | null;
  afterCursor: string | null;
}

Query next page by afterCursor

const nextPaginator = buildPaginator({
  entity: User,
  paginationKeys: ['id'],
  query: {
    limit: 10,
    order: 'ASC',
    afterCursor: cursor.afterCursor,
  },
});

const { data, cursor } = await nextPaginator.paginate(queryBuilder);

Query prev page by beforeCursor

const prevPaginator = buildPaginator({
  entity: User,
  paginationKeys: ['id'],
  query: {
    limit: 10,
    order: 'ASC',
    beforeCursor: cursor.beforeCursor,
  },
});

const { data, cursor } = await prevPaginator.paginate(queryBuilder);

Integration Test with Docker

To start an integration test, run the following command:

npm run test:docker

Contributing

All contributions are welcome, open a pull request or issue any time.

Commit your changes using a descriptive commit message that follows commit message conventions.

License

© Ben Hu (benjamin658), 2021-NOW

Released under the MIT License

typeorm-cursor-pagination's People

Contributors

benjamin658 avatar dependabot[bot] avatar eoin-obrien avatar miohtama avatar gregegan avatar jkalberer 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.