mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 07:03:38 +00:00
add and use loadPages
This commit is contained in:
parent
0f7852d8d8
commit
a17b3a0ba2
@ -186,7 +186,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
const queryParams = {
|
||||
select: fieldsToSelect,
|
||||
fields: fieldsToSelect,
|
||||
};
|
||||
|
||||
if (!isEmpty(idsToOmit)) {
|
||||
@ -194,30 +194,22 @@ module.exports = {
|
||||
}
|
||||
|
||||
if (MANY_RELATIONS.includes(attribute.relation)) {
|
||||
const page = Number(query.page || 1);
|
||||
const pageSize = Number(query.pageSize || 10);
|
||||
const res = await strapi.entityService.loadPages(sourceModelUid, { id }, targetField, {
|
||||
...queryParams,
|
||||
page: query.page,
|
||||
pageSize: query.pageSize,
|
||||
ordering: 'desc',
|
||||
});
|
||||
|
||||
queryParams.offset = Math.max(page - 1, 0) * pageSize;
|
||||
queryParams.limit = pageSize;
|
||||
|
||||
const [results, count] = await Promise.all([
|
||||
strapi.db
|
||||
.query(sourceModelUid)
|
||||
.load({ id }, targetField, { ...queryParams, ordering: 'desc' }),
|
||||
strapi.db.query(sourceModelUid).load({ id }, targetField, { ...queryParams, count: true }),
|
||||
]);
|
||||
|
||||
ctx.body = {
|
||||
results,
|
||||
pagination: {
|
||||
page: Number(query.page) || 1,
|
||||
pageSize: Number(query.pageSize) || 10,
|
||||
pageCount: results.length,
|
||||
total: count,
|
||||
},
|
||||
};
|
||||
ctx.body = res;
|
||||
} else {
|
||||
const result = await strapi.db.query(sourceModelUid).load({ id }, targetField, queryParams);
|
||||
const result = await strapi.entityService.load(
|
||||
sourceModelUid,
|
||||
{ id },
|
||||
targetField,
|
||||
queryParams
|
||||
);
|
||||
// const result = await strapi.db.query(sourceModelUid).load({ id }, targetField, queryParams);
|
||||
// TODO: Temporary fix (use data instead)
|
||||
ctx.body = {
|
||||
results: result ? [result] : [],
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { isString } = require('lodash/fp');
|
||||
const { isAnyToMany } = require('../metadata/relations');
|
||||
|
||||
const withDefaultPagination = (params) => {
|
||||
const { page = 1, pageSize = 10, ...rest } = params;
|
||||
|
||||
@ -10,6 +13,21 @@ const withDefaultPagination = (params) => {
|
||||
};
|
||||
};
|
||||
|
||||
const withOffsetLimit = (params) => {
|
||||
const { page, pageSize, ...rest } = withDefaultPagination(params);
|
||||
|
||||
const offset = Math.max(page - 1, 0) * pageSize;
|
||||
const limit = pageSize;
|
||||
|
||||
const query = {
|
||||
...rest,
|
||||
limit,
|
||||
offset,
|
||||
};
|
||||
|
||||
return [query, { page, pageSize }];
|
||||
};
|
||||
|
||||
const createRepository = (uid, db) => {
|
||||
return {
|
||||
findOne(params) {
|
||||
@ -28,16 +46,7 @@ const createRepository = (uid, db) => {
|
||||
},
|
||||
|
||||
async findPage(params) {
|
||||
const { page, pageSize, ...rest } = withDefaultPagination(params);
|
||||
|
||||
const offset = Math.max(page - 1, 0) * pageSize;
|
||||
const limit = pageSize;
|
||||
|
||||
const query = {
|
||||
...rest,
|
||||
limit,
|
||||
offset,
|
||||
};
|
||||
const [query, { page, pageSize }] = withOffsetLimit(params);
|
||||
|
||||
const [results, total] = await Promise.all([
|
||||
db.entityManager.findMany(uid, query),
|
||||
@ -99,8 +108,38 @@ const createRepository = (uid, db) => {
|
||||
return db.entityManager.populate(uid, entity, populate);
|
||||
},
|
||||
|
||||
load(entity, field, params) {
|
||||
return db.entityManager.load(uid, entity, field, params);
|
||||
load(entity, fields, params) {
|
||||
return db.entityManager.load(uid, entity, fields, params);
|
||||
},
|
||||
|
||||
async loadPages(entity, field, params) {
|
||||
if (!isString(field)) {
|
||||
throw new Error(`Invalid load. Expected ${field} to be a string`);
|
||||
}
|
||||
|
||||
const { attributes } = db.metadata.get(uid);
|
||||
const attribute = attributes[field];
|
||||
|
||||
if (!attribute || attribute.type !== 'relation' || !isAnyToMany(attribute)) {
|
||||
throw new Error(`Invalid load. Expected ${field} to be an anyToMany relational attribute`);
|
||||
}
|
||||
|
||||
const [query, { page, pageSize }] = withOffsetLimit(params);
|
||||
|
||||
const [results, { count: total }] = await Promise.all([
|
||||
db.entityManager.load(uid, entity, field, query),
|
||||
db.entityManager.load(uid, entity, field, { ...query, count: true }),
|
||||
]);
|
||||
|
||||
return {
|
||||
results,
|
||||
pagination: {
|
||||
page,
|
||||
pageSize,
|
||||
pageCount: Math.ceil(total / pageSize),
|
||||
total,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@ -275,6 +275,21 @@ const createDefaultImplementation = ({ strapi, db, eventHub, entityValidator })
|
||||
|
||||
return db.query(uid).load(entity, field, loadParams);
|
||||
},
|
||||
|
||||
loadPages(uid, entity, field, params = {}) {
|
||||
if (!_.isString(field)) {
|
||||
throw new Error(`Invalid load. Expected ${field} to be a string`);
|
||||
}
|
||||
|
||||
const { attributes } = strapi.getModel(uid);
|
||||
const attribute = attributes[field];
|
||||
|
||||
if (!attribute || attribute.type !== 'relation') {
|
||||
throw new Error(`Invalid load. Expected ${field} to be an anyToMany relational attribute`);
|
||||
}
|
||||
|
||||
return db.query(uid).loadPages(entity, field, transformParamsToQuery(attribute.target, params));
|
||||
},
|
||||
});
|
||||
|
||||
module.exports = (ctx) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user