feat: pagination type

This commit is contained in:
ppetzold 2023-03-24 12:06:56 +01:00
parent 0abc446606
commit 12dc1b0412
2 changed files with 26 additions and 1 deletions

View File

@ -256,6 +256,19 @@ const paginateConfig: PaginateConfig<CatEntity> {
*/
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

View File

@ -55,6 +55,11 @@ export class Paginated<T> {
}
}
export enum PaginationType {
LIMIT_AND_OFFSET = 'limit',
TAKE_AND_SKIP = 'take',
}
export interface PaginateConfig<T> {
relations?: FindOptionsRelations<T> | RelationColumn<T>[]
sortableColumns: Column<T>[]
@ -70,6 +75,7 @@ export interface PaginateConfig<T> {
}
loadEagerRelations?: boolean
withDeleted?: boolean
paginationType?: PaginationType
relativePath?: boolean
origin?: string
}
@ -107,7 +113,13 @@ export async function paginate<T extends ObjectLiteral>(
}
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) {