Closed Bodokh closed 2 years ago
I could use filter for specific fields like this https://test.com/api/users/me?populate[kendaraans][populate][0]=front_image
Hi, thanks for the input but for now im not going to add any features to the plugin.
Needed similar thing but reversed. So I wanted to exclude some fields. Created a hacky patch for this. Works fine for me 👍
Usage: /api/foo?populate=deep,4&exclude=your_field
or /api/foo?populate=deep,4&exclude=field1,field2,field3
diff --git a/node_modules/strapi-plugin-populate-deep/server/bootstrap.js b/node_modules/strapi-plugin-populate-deep/server/bootstrap.js
index 6d8a0e9..dc4b5a9 100644
--- a/node_modules/strapi-plugin-populate-deep/server/bootstrap.js
+++ b/node_modules/strapi-plugin-populate-deep/server/bootstrap.js
@@ -7,10 +7,17 @@ module.exports = ({ strapi }) => {
if (event.action === 'beforeFindMany' || event.action === 'beforeFindOne') {
const populate = event.params?.populate;
const defaultDepth = strapi.plugin('strapi-plugin-populate-deep')?.config('defaultDepth') || 5
-
+ const ctx = strapi.requestContext.get();
+ const exclude = []
+ if(ctx?.request?.url) {
+ const match = ctx.request.url.match(/exclude=([a-zA-Z0-9\,]+)/)
+ if(match) {
+ exclude.push(...match[1].split(','))
+ }
+ }
if (populate && populate[0] === 'deep') {
const depth = populate[1] ?? defaultDepth
- const modelObject = getFullPopulateObject(event.model.uid, depth);
+ const modelObject = getFullPopulateObject(event.model.uid, depth, exclude);
event.params.populate = modelObject.populate
}
}
diff --git a/node_modules/strapi-plugin-populate-deep/server/helpers/index.js b/node_modules/strapi-plugin-populate-deep/server/helpers/index.js
index fac6201..faf8a69 100644
--- a/node_modules/strapi-plugin-populate-deep/server/helpers/index.js
+++ b/node_modules/strapi-plugin-populate-deep/server/helpers/index.js
@@ -9,7 +9,7 @@ const getModelPopulationAttributes = (model) => {
return model.attributes;
};
-const getFullPopulateObject = (modelUid, maxDepth = 20) => {
+const getFullPopulateObject = (modelUid, maxDepth = 20, exclude = []) => {
const skipCreatorFields = strapi.plugin('strapi-plugin-populate-deep')?.config('skipCreatorFields');
if (maxDepth <= 1) {
@@ -24,6 +24,9 @@ const getFullPopulateObject = (modelUid, maxDepth = 20) => {
for (const [key, value] of Object.entries(
getModelPopulationAttributes(model)
)) {
+ if(exclude.includes(key)) {
+ continue;
+ }
if (value) {
if (value.type === "component") {
populate[key] = getFullPopulateObject(value.component, maxDepth - 1);
It would be very nice to have an option to use the deep population only for specific fields/properties