mirror of
https://github.com/immich-app/immich.git
synced 2025-01-10 13:56:47 +01:00
21 lines
667 B
TypeScript
21 lines
667 B
TypeScript
|
import { Paginated, PaginationOptions } from '@app/domain';
|
||
|
import { FindOneOptions, ObjectLiteral, Repository } from 'typeorm';
|
||
|
|
||
|
export async function paginate<Entity extends ObjectLiteral>(
|
||
|
repository: Repository<Entity>,
|
||
|
paginationOptions: PaginationOptions,
|
||
|
searchOptions?: FindOneOptions<Entity>,
|
||
|
): Paginated<Entity> {
|
||
|
const items = await repository.find({
|
||
|
...searchOptions,
|
||
|
// Take one more item to check if there's a next page
|
||
|
take: paginationOptions.take + 1,
|
||
|
skip: paginationOptions.skip,
|
||
|
});
|
||
|
|
||
|
const hasNextPage = items.length > paginationOptions.take;
|
||
|
items.splice(paginationOptions.take);
|
||
|
|
||
|
return { items, hasNextPage };
|
||
|
}
|