backend/lib/seed/helpers/Request.ts
2025-05-14 21:45:16 +02:00

30 lines
946 B
TypeScript

import { GetArgs } from '@seed/graphql/Request';
const DEFAULT_LIMIT = 100;
const DEFAULT_OFFSET = 0;
export interface QueryPagination {
limit: number;
skip: number;
sort?: {
[k: string]: 1 | -1;
};
}
export const parsePaginationOptions = (obj?: GetArgs): QueryPagination => {
const limit = obj ? (obj.limit ? obj.limit : DEFAULT_LIMIT) : DEFAULT_LIMIT;
const skip = obj ? (obj.skip ? obj.skip : DEFAULT_OFFSET) : DEFAULT_OFFSET;
if (obj && obj.sort) {
const sort = {};
const sortArray = ('' + obj.sort).split(',');
for (let index = 0; index < sortArray.length; index++) {
const sortTmp = ('' + obj.sort).split(' ');
if (sortTmp[1].toLowerCase() === 'asc') (sort as any)[sortTmp[0]] = 1;
if (sortTmp[1].toLowerCase() === 'desc') (sort as any)[sortTmp[0]] = -1;
}
return { limit, skip, sort };
}
return { limit, skip };
};