async-pagination
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Async Pagination

This library is meant for Node.js applications that need to paginate data from a database plus another other data source.

  • Simple: It's just a function that returns an iterator.
  • Flexible: It doesn't care about your database or ORM, or data fetching library.

The problem

Paginating records stting in a database is a fairly common task supported by most ORMs.

But when you need to fetch records, and then filter that data using another data source, things get a little more complicated. You end up with less results per page than you expected.

This makes for a confusing user experience, as they are getting inconsistent number of results per page, and even no results at all on certain pages.

The solution

This library provides you with a class that you can use to paginate data from a database, plus any arbitrary number of filters.

You can use those filters to fetch data from other sources, like an API, or a file.

After applying the filters, if the number of results is less than the page size, it will fetch more data from the database.

It will stop fetching data when it has enough results to fill the page, or when it has reached the end of the database results.

Installation

npm install async-pagination

Usage

import { AsyncPagination } from 'async-pagination';

const paginator = new AsyncPagination(cursor, pageSize);

// Register your filters
paginator.filter(async (results) => {
  // Apply filters to your results
  return filteredResults;
});

// You can add as many filters as you want
// They will be applied in the order they were added

const iterator = await paginator.fetch(
  // This is the function that will fetch data from your database
  async (cursor, pageSize) => {
    // Fetch data from your database
    return results;
  },
  // This is used to find the next cursor
  (results, lastCursor) => {
    // Based on the results, return the next cursor
    // For example, you can take the last result's ID
    return yourNextCursor;
  }
);

// One iteration will return a page of results
// If you want a single page, you can use the .next() method
const { value } = await iterator.next();

// If you want to iterate over all the pages, you can use a for loop
for await (const page of iterator) {
  // Do something with the page of results
}

Readme

Keywords

none

Package Sidebar

Install

npm i async-pagination

Weekly Downloads

6

Version

1.2.0

License

UNLICENSED

Unpacked Size

28.5 kB

Total Files

15

Last publish

Collaborators

  • ahmet_mintubi