UNPKG

typeorm-cursor-paginate

Version:

Cursor-based pagination with directional cursors.

78 lines (77 loc) 2.77 kB
import { SelectQueryBuilder, ObjectType, ObjectLiteral } from "typeorm"; import { Paginated, OrderBy, CursorTransformer } from "./interfaces/paginator"; export interface CursorPaginatorParams<TEntity extends ObjectLiteral> { /** * Columns to order by. * * CAUTION: the set of provided columns must be unique in database, * otherwise entries might be skipped/duplicated. This rule is not enforced * by the library, so be careful. */ orderBy: OrderBy<TEntity> | OrderBy<TEntity>[]; /** * Transformer to use for the cursor stringification and parsing. * * By default, a `Base64Transformer` is used. * * There is also a predefined `JsonTransformer` you can use. */ transformer?: CursorTransformer<TEntity> | null; } export interface CursorPaginatorPaginateParams { /** * The cursor to the next or previous page. * * Use `prevPageCursor` or `nextPageCursor` which you got from * the previous page. */ pageCursor?: string | null; /** * The maximum number of items to return in the current page. * * If not provided, will return all remaining items. */ limit?: number; /** * If true, the total count will not be calculated. * * This can be useful for improving performance. * The default value is false (the total count is calculated). */ noTotalCount?: boolean; } /** * A class that implements a cursor-based pagination for TypeORM. * * @template TEntity The type of the entity that is being selected from the database. */ export declare class CursorPaginator<TEntity extends ObjectLiteral> { readonly entity: ObjectType<TEntity>; private _orders; private _transformer; /** * Creates a new instance of the `CursorPaginator` class. * * @param entity The entity type to paginate. * @param options The options for the paginator. */ constructor(entity: ObjectType<TEntity>, options: CursorPaginatorParams<TEntity>); /** * Paginate the results of a query builder using the directional cursor. * * @param qb The query builder to paginate. * @param params The pagination parameters. * @param isRaw If true, the raw results will be returned. */ paginate(qb: SelectQueryBuilder<TEntity>, params?: CursorPaginatorPaginateParams, isRaw?: boolean): Promise<Paginated<TEntity>>; private _applyWhereQuery; private _createCursor; private _stringifyCursor; private _parseCursor; /** * Checks that all properties in the cursor are in the orderBy and * that all properties in the orderBy are in the cursor. * Also checks that no property in the cursor is undefined. */ private _validateCursor; }