remove relations populate configuration

This commit is contained in:
Marc-Roig 2023-01-12 14:31:04 +01:00
parent bf3c7e6a9d
commit cea950fd96
2 changed files with 26 additions and 65 deletions

View File

@ -13,8 +13,4 @@ module.exports = ({ env }) => ({
app: { app: {
keys: env.array('APP_KEYS', ['toBeModified1', 'toBeModified2']), keys: env.array('APP_KEYS', ['toBeModified1', 'toBeModified2']),
}, },
// Receive populated relations in webhook and db lifecycle payloads
relations: {
populate: true,
},
}); });

View File

@ -26,14 +26,6 @@ const wrapWithEmitEvent = (event, fn) => async (entity, body, model) => {
entry: sanitizedEntity, 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; 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} * @type {import('./entity-manager').default}
*/ */
@ -127,7 +107,6 @@ module.exports = ({ strapi }) => ({
async create(body, uid) { async create(body, uid) {
const modelDef = strapi.getModel(uid); const modelDef = strapi.getModel(uid);
const publishData = { ...body }; const publishData = { ...body };
const populateRelations = isRelationsPopulateEnabled(uid);
if (hasDraftAndPublish(modelDef)) { if (hasDraftAndPublish(modelDef)) {
publishData[PUBLISHED_AT_ATTRIBUTE] = null; publishData[PUBLISHED_AT_ATTRIBUTE] = null;
@ -135,57 +114,45 @@ module.exports = ({ strapi }) => ({
const params = { const params = {
data: publishData, data: publishData,
populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), populate: getDeepPopulate(uid, {}),
}; };
const entity = await strapi.entityService.create(uid, params); const entity = await strapi.entityService.create(uid, params);
// If relations were populated, load the entity again without populating them, // If relations were populated, load the entity again without populating them,
// to avoid performance issues // to avoid performance issues
if (populateRelations) { return strapi.entityService.findOne(uid, entity.id, {
return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) }); populate: getDeepPopulate(uid, { countMany: true, countOne: true }),
} });
return entity;
}, },
async update(entity, body, uid) { async update(entity, body, uid) {
const publishData = omitPublishedAtField(body); const publishData = omitPublishedAtField(body);
const populateRelations = isRelationsPopulateEnabled(uid);
const params = { const params = {
data: publishData, 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, return strapi.entityService.findOne(uid, entity.id, {
// to avoid performance issues populate: getDeepPopulate(uid, { countMany: true, countOne: true }),
if (populateRelations) { });
return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) });
}
return updatedEntity;
}, },
async delete(entity, uid) { async delete(entity, uid) {
let entityToDelete;
const populateRelations = isRelationsPopulateEnabled(uid);
const params = { const params = {
populate: populateRelations ? getDeepPopulate(uid, {}) : getCountDeepPopulate(uid), populate: getDeepPopulate(uid, {}),
}; };
if (populateRelations) { const deletedEntity = await strapi.entityService.findOne(uid, entity.id, {
entityToDelete = await strapi.entityService.findOne(uid, entity.id, { populate: getDeepPopulate(uid, { countMany: true, countOne: true }),
populate: getCountDeepPopulate(uid), });
});
}
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 // FIXME: handle relations
@ -209,22 +176,17 @@ module.exports = ({ strapi }) => ({
); );
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: new Date() }; const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: new Date() };
const populateRelations = isRelationsPopulateEnabled(uid);
const params = { const params = {
data, 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, return strapi.entityService.findOne(uid, entity.id, {
// to avoid performance issues populate: getDeepPopulate(uid, { countMany: true, countOne: true }),
if (populateRelations) { });
return strapi.entityService.findOne(uid, entity.id, { populate: getCountDeepPopulate(uid) });
}
return updatedEntity;
}), }),
unpublish: wrapWithEmitEvent(ENTRY_UNPUBLISH, async (entity, body = {}, uid) => { unpublish: wrapWithEmitEvent(ENTRY_UNPUBLISH, async (entity, body = {}, uid) => {
@ -233,14 +195,17 @@ module.exports = ({ strapi }) => ({
} }
const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: null }; const data = { ...body, [PUBLISHED_AT_ATTRIBUTE]: null };
const populateRelations = isRelationsPopulateEnabled(uid);
const params = { const params = {
data, 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) { async getNumberOfDraftRelations(id, uid) {