From cea950fd9657df6434d22ecd29c1bbfd8d9a0912 Mon Sep 17 00:00:00 2001 From: Marc-Roig Date: Thu, 12 Jan 2023 14:31:04 +0100 Subject: [PATCH] remove relations populate configuration --- examples/getstarted/config/server.js | 4 - .../server/services/entity-manager.js | 87 ++++++------------- 2 files changed, 26 insertions(+), 65 deletions(-) diff --git a/examples/getstarted/config/server.js b/examples/getstarted/config/server.js index 5aad8d368a..8fdd269cec 100644 --- a/examples/getstarted/config/server.js +++ b/examples/getstarted/config/server.js @@ -13,8 +13,4 @@ module.exports = ({ env }) => ({ app: { keys: env.array('APP_KEYS', ['toBeModified1', 'toBeModified2']), }, - // Receive populated relations in webhook and db lifecycle payloads - relations: { - populate: true, - }, }); diff --git a/packages/core/content-manager/server/services/entity-manager.js b/packages/core/content-manager/server/services/entity-manager.js index 065792984d..a7bef92a07 100644 --- a/packages/core/content-manager/server/services/entity-manager.js +++ b/packages/core/content-manager/server/services/entity-manager.js @@ -26,14 +26,6 @@ const wrapWithEmitEvent = (event, fn) => async (entity, body, model) => { entry: sanitizedEntity, }); - // If relations were populated, load the entity again without populating them, - // to avoid performance issues - if (isRelationsPopulateEnabled(model)) { - return strapi.entityService.findOne(model, entity.id, { - populate: getCountDeepPopulate(model), - }); - } - return result; }; @@ -57,18 +49,6 @@ const addCreatedByRolesPopulate = (populate) => { }; }; -/** - * When relations.populate is set to true, populated relations - * will be passed to the webhook and db lifecycles events. The entity-manager - * response will not have the populated relations though. - * For performance reasons, it is recommended to set it to false, - */ -const isRelationsPopulateEnabled = () => { - return strapi.config.get('server.relations.populate', false); -}; - -const getCountDeepPopulate = (uid) => getDeepPopulate(uid, { countMany: true, countOne: true }); - /** * @type {import('./entity-manager').default} */ @@ -127,7 +107,6 @@ module.exports = ({ strapi }) => ({ async create(body, uid) { const modelDef = strapi.getModel(uid); const publishData = { ...body }; - const populateRelations = isRelationsPopulateEnabled(uid); if (hasDraftAndPublish(modelDef)) { publishData[PUBLISHED_AT_ATTRIBUTE] = null; @@ -135,57 +114,45 @@ module.exports = ({ strapi }) => ({ const params = { data: publishData, - populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), + populate: getDeepPopulate(uid, {}), }; const entity = await strapi.entityService.create(uid, params); // If relations were populated, load the entity again without populating them, // to avoid performance issues - if (populateRelations) { - return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) }); - } - - return entity; + return strapi.entityService.findOne(uid, entity.id, { + populate: getDeepPopulate(uid, { countMany: true, countOne: true }), + }); }, async update(entity, body, uid) { const publishData = omitPublishedAtField(body); - const populateRelations = isRelationsPopulateEnabled(uid); const params = { data: publishData, - populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), + populate: getDeepPopulate(uid, {}), }; - const updatedEntity = await strapi.entityService.update(uid, entity.id, params); + await strapi.entityService.update(uid, entity.id, params); - // If relations were populated, load the entity again without populating them, - // to avoid performance issues - if (populateRelations) { - return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) }); - } - - return updatedEntity; + return strapi.entityService.findOne(uid, entity.id, { + populate: getDeepPopulate(uid, { countMany: true, countOne: true }), + }); }, async delete(entity, uid) { - let entityToDelete; - const populateRelations = isRelationsPopulateEnabled(uid); - const params = { - populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), + populate: getDeepPopulate(uid, {}), }; - if (populateRelations) { - entityToDelete = await strapi.entityService.findOne(uid, entity.id, { - populate: getCountDeepPopulate(uid), - }); - } + const deletedEntity = await strapi.entityService.findOne(uid, entity.id, { + populate: getDeepPopulate(uid, { countMany: true, countOne: true }), + }); - const deletedEntity = await strapi.entityService.delete(uid, entity.id, params); + await strapi.entityService.delete(uid, entity.id, params); - return entityToDelete || deletedEntity; + return deletedEntity; }, // FIXME: handle relations @@ -209,22 +176,17 @@ module.exports = ({ strapi }) => ({ ); const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: new Date() }; - const populateRelations = isRelationsPopulateEnabled(uid); const params = { data, - populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), + populate: getDeepPopulate(uid, {}), }; - const updatedEntity = await strapi.entityService.update(uid, entity.id, params); + await strapi.entityService.update(uid, entity.id, params); - // If relations were populated, load the entity again without populating them, - // to avoid performance issues - if (populateRelations) { - return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) }); - } - - return updatedEntity; + return strapi.entityService.findOne(uid, entity.id, { + populate: getDeepPopulate(uid, { countMany: true, countOne: true }), + }); }), unpublish: wrapWithEmitEvent(ENTRY_UNPUBLISH, async (entity, body = {}, uid) => { @@ -233,14 +195,17 @@ module.exports = ({ strapi }) => ({ } const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: null }; - const populateRelations = isRelationsPopulateEnabled(uid); const params = { data, - populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), + populate: getDeepPopulate(uid, {}), }; - return strapi.entityService.update(uid, entity.id, params); + await strapi.entityService.update(uid, entity.id, params); + + return strapi.entityService.findOne(uid, entity.id, { + populate: getDeepPopulate(uid, { countMany: true, countOne: true }), + }); }), async getNumberOfDraftRelations(id, uid) {