Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: document service find many pagination #20178

Merged
merged 14 commits into from May 6, 2024
13 changes: 12 additions & 1 deletion packages/core/database/src/entity-manager/entity-repository.ts
@@ -1,4 +1,4 @@
import { isString } from 'lodash/fp';
import { isString, isNil } from 'lodash/fp';
import type { Database } from '..';
import type { Repository, Params } from './types';

Expand All @@ -20,6 +20,17 @@ type ParamsWithLimits = Omit<Params, 'page' | 'pageSize'> & {
const withOffsetLimit = (
params: Params
): [ParamsWithLimits, { page: number; pageSize: number }] => {
// If params already include limit and offset, return them as is
if (!isNil(params.limit) && !isNil(params.offset)) {
return [
params as ParamsWithLimits,
{
page: Math.floor(params.offset / params.limit) + 1,
pageSize: params.limit,
},
];
}

const { page, pageSize, ...rest } = withDefaultPagination(params);

const offset = Math.max(page - 1, 0) * pageSize;
Expand Down
42 changes: 21 additions & 21 deletions packages/core/utils/src/convert-query-params.ts
Expand Up @@ -247,28 +247,32 @@ const createTransformer = ({ getModel }: TransformerOptions) => {
return limitAsANumber;
};

const convertPageQueryParams = (page: unknown): number => {
const pageVal = toNumber(page);
const convertPageQueryToOffsetLimit = (
page: unknown,
pageSize?: unknown
): {
offset: number;
limit: number;
} => {
const pageVal = toNumber(page) ?? 1;
const pageSizeVal = toNumber(pageSize) ?? 10;

if (!isInteger(pageVal) || pageVal <= 0) {
throw new PaginationError(
`Invalid 'page' parameter. Expected an integer > 0, received: ${page}`
);
}

return pageVal;
};

const convertPageSizeQueryParams = (pageSize: unknown, page: unknown): number => {
const pageSizeVal = toNumber(pageSize);

if (!isInteger(pageSizeVal) || pageSizeVal <= 0) {
throw new PaginationError(
`Invalid 'pageSize' parameter. Expected an integer > 0, received: ${page}`
);
}

return pageSizeVal;
const offset = Math.max(pageVal - 1, 0) * pageSizeVal;
const limit = pageSizeVal;

return { offset, limit };
};

const validatePaginationParams = (
Expand Down Expand Up @@ -474,12 +478,10 @@ const createTransformer = ({ getModel }: TransformerOptions) => {

validatePaginationParams(page, pageSize, start, limit);

if (!isNil(page)) {
query.page = convertPageQueryParams(page);
}

if (!isNil(pageSize)) {
query.pageSize = convertPageSizeQueryParams(pageSize, page);
if (!isNil(page) || !isNil(pageSize)) {
const { offset, limit } = convertPageQueryToOffsetLimit(page, pageSize);
query.offset = offset;
query.limit = limit;
}

if (!isNil(start)) {
Expand Down Expand Up @@ -665,12 +667,10 @@ const createTransformer = ({ getModel }: TransformerOptions) => {

validatePaginationParams(page, pageSize, start, limit);

if (!isNil(page)) {
query.page = convertPageQueryParams(page);
}

if (!isNil(pageSize)) {
query.pageSize = convertPageSizeQueryParams(pageSize, page);
if (!isNil(page) || !isNil(pageSize)) {
const { offset, limit } = convertPageQueryToOffsetLimit(page, pageSize);
query.offset = offset;
query.limit = limit;
}

if (!isNil(start)) {
Expand Down