From 8335560c3adc9f1f9e161a9b6460024febe4cb4e Mon Sep 17 00:00:00 2001 From: "jacopo.bonomi" Date: Thu, 6 Oct 2022 17:57:19 +0200 Subject: [PATCH 1/2] feat: add params to manage default and max populate depth --- package.json | 6 +++--- server/bootstrap.js | 2 +- server/helpers/index.js | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 23af98a..84a4360 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "strapi-plugin-populate-deep", - "version": "0.1.2", + "name": "strapi-plugin-populate-deep-with-params", + "version": "0.1.3", "description": "This is the description of the plugin.", "strapi": { "name": "strapi-plugin-populate-deep", @@ -18,7 +18,7 @@ ], "repository": { "type": "git", - "url": "https://github.com/Barelydead/strapi-plugin-deep-populate" + "url": "https://github.com/jacopobonomi/strapi-plugin-deep-populate" }, "engines": { "node": ">=12.x. <=16.x.x", diff --git a/server/bootstrap.js b/server/bootstrap.js index 53d38dc..d358efd 100644 --- a/server/bootstrap.js +++ b/server/bootstrap.js @@ -8,7 +8,7 @@ module.exports = ({ strapi }) => { const populate = event.params?.populate; if (populate && populate[0] === 'deep') { - const depth = populate[1] ?? 5 + const depth = populate[1] ?? process.env.DEFAULT_POPULATE_DEPTH ?? 5; const modelObject = getFullPopulateObject(event.model.uid, depth); event.params.populate = modelObject.populate } diff --git a/server/helpers/index.js b/server/helpers/index.js index ed15108..583a431 100644 --- a/server/helpers/index.js +++ b/server/helpers/index.js @@ -9,7 +9,10 @@ const getModelPopulationAttributes = (model) => { return model.attributes; }; -const getFullPopulateObject = (modelUid, maxDepth = 20) => { +const getFullPopulateObject = ( + modelUid, + maxDepth = process.env.MAX_POPULATE_DEPTH ?? 20 +) => { if (maxDepth <= 1) { return true; } @@ -48,5 +51,5 @@ const getFullPopulateObject = (modelUid, maxDepth = 20) => { }; module.exports = { - getFullPopulateObject -} \ No newline at end of file + getFullPopulateObject, +}; From 6159846fc19d5ef1f879f910868476e6f1341569 Mon Sep 17 00:00:00 2001 From: "jacopo.bonomi" Date: Thu, 6 Oct 2022 18:06:51 +0200 Subject: [PATCH 2/2] fix: add condition to limit number of deep --- package.json | 2 +- server/helpers/index.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 84a4360..da91d80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "strapi-plugin-populate-deep-with-params", - "version": "0.1.3", + "version": "0.1.4", "description": "This is the description of the plugin.", "strapi": { "name": "strapi-plugin-populate-deep", diff --git a/server/helpers/index.js b/server/helpers/index.js index 583a431..134a186 100644 --- a/server/helpers/index.js +++ b/server/helpers/index.js @@ -13,6 +13,11 @@ const getFullPopulateObject = ( modelUid, maxDepth = process.env.MAX_POPULATE_DEPTH ?? 20 ) => { + + if (maxDepth > process.env.MAX_POPULATE_DEPTH) { + maxDepth = process.env.MAX_POPULATE_DEPTH; + } + if (maxDepth <= 1) { return true; }