mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 19:36:20 +00:00
fix: make pagination work properly
This commit is contained in:
parent
efeb53f7ab
commit
0d380ed3ae
@ -1,4 +1,4 @@
|
|||||||
import { prop, isEmpty, uniq, flow, concat, uniqBy } from 'lodash/fp';
|
import { prop, isEmpty, uniq, flow, uniqBy, concat } from 'lodash/fp';
|
||||||
import { isOperatorOfType, contentTypes, relations } from '@strapi/utils';
|
import { isOperatorOfType, contentTypes, relations } from '@strapi/utils';
|
||||||
import { getService } from '../utils';
|
import { getService } from '../utils';
|
||||||
import { validateFindAvailable, validateFindExisting } from './validation/relations';
|
import { validateFindAvailable, validateFindExisting } from './validation/relations';
|
||||||
@ -254,34 +254,40 @@ export default {
|
|||||||
const permissionQuery = await permissionChecker.sanitizedQuery.read(queryParams);
|
const permissionQuery = await permissionChecker.sanitizedQuery.read(queryParams);
|
||||||
|
|
||||||
if (isAnyToMany(attribute)) {
|
if (isAnyToMany(attribute)) {
|
||||||
const resWithIds = await strapi.entityService.loadPages(
|
const [resWithIds, res] = await Promise.all([
|
||||||
model,
|
strapi.entityService.loadPages(
|
||||||
{ id },
|
model,
|
||||||
targetField,
|
{ id },
|
||||||
{
|
targetField,
|
||||||
fields: ['id'],
|
{
|
||||||
} as any,
|
fields: ['id'],
|
||||||
{
|
} as any,
|
||||||
page: ctx.request.query.page,
|
{
|
||||||
pageSize: ctx.request.query.pageSize,
|
page: ctx.request.query.page,
|
||||||
}
|
pageSize: ctx.request.query.pageSize,
|
||||||
);
|
}
|
||||||
|
),
|
||||||
|
strapi.entityService.loadPages(
|
||||||
|
model,
|
||||||
|
{ id },
|
||||||
|
targetField,
|
||||||
|
{
|
||||||
|
...permissionQuery,
|
||||||
|
ordering: 'desc',
|
||||||
|
} as any,
|
||||||
|
{
|
||||||
|
page: ctx.request.query.page,
|
||||||
|
pageSize: ctx.request.query.pageSize,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
const res = await strapi.entityService.loadPages(
|
// Filter out the results that don't exist in resWithIds to preserve correct pagination
|
||||||
model,
|
const results = res.results.filter((item: any) =>
|
||||||
{ id },
|
resWithIds.results.some((s: any) => item.id === s.id)
|
||||||
targetField,
|
|
||||||
{
|
|
||||||
...permissionQuery,
|
|
||||||
ordering: 'desc',
|
|
||||||
} as any,
|
|
||||||
{
|
|
||||||
page: ctx.request.query.page,
|
|
||||||
pageSize: ctx.request.query.pageSize,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
res.results = uniqBy('id', concat(results, resWithIds.results));
|
||||||
|
|
||||||
res.results = uniqBy('id', concat(res.results, resWithIds.results));
|
|
||||||
ctx.body = res;
|
ctx.body = res;
|
||||||
} else {
|
} else {
|
||||||
const result = await strapi.entityService.load(model, { id }, targetField, queryParams);
|
const result = await strapi.entityService.load(model, { id }, targetField, queryParams);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user