feat: Implement pagination logic and use with getExecutions query
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
906a8d0644
commit
44623cc384
30
packages/backend/src/helpers/pagination.ts
Normal file
30
packages/backend/src/helpers/pagination.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { QueryBuilder, Model } from 'objection';
|
||||
|
||||
const paginate = async (
|
||||
query: QueryBuilder<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),
|
||||
},
|
||||
edges: records.map((record: Model) => {
|
||||
return {
|
||||
node: record,
|
||||
};
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
export default paginate;
|
Reference in New Issue
Block a user