also count xToOne relations in CM findOne

This commit is contained in:
Pierre Noël 2022-10-17 18:44:33 +02:00
parent b72360b225
commit 4ee9757ec3
3 changed files with 15 additions and 13 deletions

View File

@ -82,7 +82,7 @@ module.exports = ({ strapi }) => ({
}, },
findOneWithCreatorRolesAndCount(id, uid, populate) { findOneWithCreatorRolesAndCount(id, uid, populate) {
const counterPopulate = getDeepPopulate(uid, populate, { onlyMany: true, countMany: true }); const counterPopulate = getDeepPopulate(uid, populate, { countMany: true, countOne: true });
const params = { populate: addCreatedByRolesPopulate(counterPopulate) }; const params = { populate: addCreatedByRolesPopulate(counterPopulate) };
return strapi.entityService.findOne(uid, id, params); return strapi.entityService.findOne(uid, id, params);
@ -114,7 +114,7 @@ module.exports = ({ strapi }) => ({
const params = { const params = {
data: publishData, data: publishData,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }), populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
}; };
return strapi.entityService.create(uid, params); return strapi.entityService.create(uid, params);
@ -125,14 +125,14 @@ module.exports = ({ strapi }) => ({
const params = { const params = {
data: publishData, data: publishData,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }), populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
}; };
return strapi.entityService.update(uid, entity.id, params); return strapi.entityService.update(uid, entity.id, params);
}, },
delete(entity, uid) { delete(entity, uid) {
const params = { populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }) }; const params = { populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }) };
return strapi.entityService.delete(uid, entity.id, params); return strapi.entityService.delete(uid, entity.id, params);
}, },
@ -161,7 +161,7 @@ module.exports = ({ strapi }) => ({
const params = { const params = {
data, data,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }), populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
}; };
return strapi.entityService.update(uid, entity.id, params); return strapi.entityService.update(uid, entity.id, params);
@ -176,7 +176,7 @@ module.exports = ({ strapi }) => ({
const params = { const params = {
data, data,
populate: getDeepPopulate(uid, null, { onlyMany: true, countMany: true }), populate: getDeepPopulate(uid, null, { countMany: true, countOne: true }),
}; };
return strapi.entityService.update(uid, entity.id, params); return strapi.entityService.update(uid, entity.id, params);

View File

@ -10,7 +10,7 @@ const { PUBLISHED_AT_ATTRIBUTE } = strapiUtils.contentTypes.constants;
const getDeepPopulate = ( const getDeepPopulate = (
uid, uid,
populate, populate,
{ onlyMany = false, countMany = false, maxLevel = Infinity } = {}, { countMany = false, countOne = false, maxLevel = Infinity } = {},
level = 1 level = 1
) => { ) => {
if (populate) { if (populate) {
@ -31,9 +31,10 @@ const getDeepPopulate = (
// always populate createdBy, updatedBy, localizations etc. // always populate createdBy, updatedBy, localizations etc.
if (!isVisibleAttribute(model, attributeName)) { if (!isVisibleAttribute(model, attributeName)) {
populateAcc[attributeName] = true; populateAcc[attributeName] = true;
} else if (!onlyMany || isManyRelation) { } else if ((isManyRelation && countMany) || (!isManyRelation && countOne)) {
// Only populate one level of relations populateAcc[attributeName] = { count: true };
populateAcc[attributeName] = countMany && isManyRelation ? { count: true } : true; } else {
populateAcc[attributeName] = true;
} }
} }
@ -42,7 +43,7 @@ const getDeepPopulate = (
populate: getDeepPopulate( populate: getDeepPopulate(
attribute.component, attribute.component,
null, null,
{ onlyMany, countMany, maxLevel }, { countOne, countMany, maxLevel },
level + 1 level + 1
), ),
}; };
@ -57,7 +58,7 @@ const getDeepPopulate = (
populate: (attribute.components || []).reduce((acc, componentUID) => { populate: (attribute.components || []).reduce((acc, componentUID) => {
return merge( return merge(
acc, acc,
getDeepPopulate(componentUID, null, { onlyMany, countMany, maxLevel }, level + 1) getDeepPopulate(componentUID, null, { countOne, countMany, maxLevel }, level + 1)
); );
}, {}), }, {}),
}; };

View File

@ -211,7 +211,6 @@ describe('CM API', () => {
}); });
expect(res.statusCode).toBe(200); expect(res.statusCode).toBe(200);
// only xToMany relations are populated and counted
expect(res.body).toMatchObject({ expect(res.body).toMatchObject({
age: 25, age: 25,
id: 1, id: 1,
@ -219,6 +218,8 @@ describe('CM API', () => {
stamps: { count: 2 }, stamps: { count: 2 },
stamps_m2m: { count: 1 }, stamps_m2m: { count: 1 },
stamps_one_many: { count: 0 }, stamps_one_many: { count: 0 },
stamps_one_one: { count: 1 },
stamps_one_way: { count: 1 },
createdBy: null, createdBy: null,
updatedBy: null, updatedBy: null,
}); });