diff --git a/README.md b/README.md index d005203..1335b85 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,19 @@ const paginateConfig: PaginateConfig { */ withDeleted: false, + /** + * Required: false + * Type: string + * Description: Allow user to choose between limit/offset and take/skip. + * Default: PaginationType.LIMIT_AND_OFFSET + * + * However, using take/skip can cause problems with sorting and selections. + * For more information see: + * [#4742](https://github.com/typeorm/typeorm/issues/4742) + * [#5670](https://github.com/typeorm/typeorm/issues/5670) + */ + paginationType: PaginationType.TAKE_AND_SKIP, + /** * Required: false * Type: boolean diff --git a/src/paginate.ts b/src/paginate.ts index ebe71bb..c25ebe3 100644 --- a/src/paginate.ts +++ b/src/paginate.ts @@ -55,6 +55,11 @@ export class Paginated { } } +export enum PaginationType { + LIMIT_AND_OFFSET = 'limit', + TAKE_AND_SKIP = 'take', +} + export interface PaginateConfig { relations?: FindOptionsRelations | RelationColumn[] sortableColumns: Column[] @@ -70,6 +75,7 @@ export interface PaginateConfig { } loadEagerRelations?: boolean withDeleted?: boolean + paginationType?: PaginationType relativePath?: boolean origin?: string } @@ -107,7 +113,13 @@ export async function paginate( } if (isPaginated) { - queryBuilder.limit(limit).offset((page - 1) * limit) + // Allow user to choose between limit/offset and take/skip. + // However, using take/skip can cause problems with sorting and selections. + if (config.paginationType === PaginationType.TAKE_AND_SKIP) { + queryBuilder.take(limit).skip((page - 1) * limit) + } else { + queryBuilder.limit(limit).offset((page - 1) * limit) + } } if (config.relations) {