Files
automatisch/packages/backend/src/helpers/pagination.ts
Rıdvan Akca 163aca6179 feat(user-list): add pagination (#1219)
* feat(user-list): add pagination

* feat: add actual total count in getUsers

---------

Co-authored-by: Ali BARIN <ali.barin53@gmail.com>
2023-08-21 21:15:07 +02:00

32 lines
720 B
TypeScript

import { Model } from 'objection';
import ExtendedQueryBuilder from '../models/query-builder';
import type Base from '../models/base';
const paginate = async (
query: ExtendedQueryBuilder<Model, Model[]>,
limit: number,
offset: number,
) => {
if (limit < 1 || limit > 100) {
throw new Error('Limit must be between 1 and 100');
}
const [records, count] = await Promise.all([
query.limit(limit).offset(offset),
query.resultSize(),
]);
return {
pageInfo: {
currentPage: Math.ceil(offset / limit + 1),
totalPages: Math.ceil(count / limit),
},
totalCount: count,
edges: records.map((record: Base) => ({
node: record,
})),
};
};
export default paginate;