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: {
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,
});
// 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) {