feat: pagination type
This commit is contained in:
parent
0abc446606
commit
12dc1b0412
13
README.md
13
README.md
@ -256,6 +256,19 @@ const paginateConfig: PaginateConfig<CatEntity> {
|
|||||||
*/
|
*/
|
||||||
withDeleted: false,
|
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
|
* Required: false
|
||||||
* Type: boolean
|
* Type: boolean
|
||||||
|
@ -55,6 +55,11 @@ export class Paginated<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum PaginationType {
|
||||||
|
LIMIT_AND_OFFSET = 'limit',
|
||||||
|
TAKE_AND_SKIP = 'take',
|
||||||
|
}
|
||||||
|
|
||||||
export interface PaginateConfig<T> {
|
export interface PaginateConfig<T> {
|
||||||
relations?: FindOptionsRelations<T> | RelationColumn<T>[]
|
relations?: FindOptionsRelations<T> | RelationColumn<T>[]
|
||||||
sortableColumns: Column<T>[]
|
sortableColumns: Column<T>[]
|
||||||
@ -70,6 +75,7 @@ export interface PaginateConfig<T> {
|
|||||||
}
|
}
|
||||||
loadEagerRelations?: boolean
|
loadEagerRelations?: boolean
|
||||||
withDeleted?: boolean
|
withDeleted?: boolean
|
||||||
|
paginationType?: PaginationType
|
||||||
relativePath?: boolean
|
relativePath?: boolean
|
||||||
origin?: string
|
origin?: string
|
||||||
}
|
}
|
||||||
@ -107,7 +113,13 @@ export async function paginate<T extends ObjectLiteral>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isPaginated) {
|
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) {
|
if (config.relations) {
|
||||||
|
Loading…
Reference in New Issue
Block a user