@sesamecare-oss/multi-source-pager
TypeScript icon, indicating that this package has built-in type declarations

4.0.2 • Public • Published

@sesamecare-oss/multi-source-pager

Sometimes you need to join multiple remote data sources (such as external APIs) together into one list and support paging. @sesamecare-oss/multi-source-pager provides a class that manages these multiple streams and exposes a cursor-based paging interface to clients.

Currently, the module only supports "forward" paging, additional work is required to go backwards.

Usage

See the tests for examples, but the core thing YOU need to do is implement a datasource. A datasource does two simple things, get results from a cursor, and return the sort key for a particular result.

interface DataSource<T extends ResultWithCursor> {
  getResults(cursor: string | undefined, forward: boolean, limit: number): Promise<DatasourceResults<T>>;
  sortKey(result: T): string;
}

Underneath, we are using generators to fetch a single record at a time and avoid overfetching. So, if you're into that sort of thing, you can implement the DataGenerator instead:

export interface DataGenerator<T extends ResultWithCursor> {
  getResults(cursor: string | undefined, forward: boolean): AsyncGenerator<T, void, unknown>;
  sortKey(result: T): string;
  totalResults(): number | undefined;
}

Now, to invoke the pager, you pass some options:

  • a comparator function which will be passed the sortKey of two results and return standard comparison result values
  • a cursor for the starting point of the results
  • edgeCursorsOnly when you only want cursor values for the first and last item (this can save wire bytes)

And then just a list of data sources to use for the fetch.

You can convert a DataSource to a DataGenerator using the asDataGenerator function, which also allows you to specify a filter to be applied after the results are fetched.

THERE ARE BUGS - not because I know what they are but because this is hard and has not been battle tested nearly enough.

Readme

Keywords

Package Sidebar

Install

npm i @sesamecare-oss/multi-source-pager

Weekly Downloads

7

Version

4.0.2

License

UNLICENSED

Unpacked Size

61.5 kB

Total Files

35

Last publish

Collaborators

  • djmax