npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-pagination

v1.2.0

Published

Iteratively fetch & filter results for pagination

Downloads

26

Readme

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
}