Compare commits

..

1 Commits
main ... v1.0.2

Author SHA1 Message Date
Christofer Jungberg
b12a47478a 1.0.2 2022-10-25 22:56:16 +02:00
4 changed files with 11 additions and 14 deletions

4
package-lock.json generated
View File

@ -1,5 +1,5 @@
{
"name": "strapi-plugin-populate-deep",
"version": "3.0.1",
"version": "1.0.2",
"lockfileVersion": 1
}
}

View File

@ -1,7 +1,7 @@
{
"name": "strapi-plugin-populate-deep",
"version": "3.0.1",
"description": "Strapi plugin that populates nested content.",
"version": "1.0.2",
"description": "This is the description of the plugin.",
"strapi": {
"name": "strapi-plugin-populate-deep",
"description": "Api helper to populate deep content structures.",
@ -21,8 +21,8 @@
"url": "https://github.com/Barelydead/strapi-plugin-deep-populate"
},
"engines": {
"node": ">=14.19.1 <=20.x.x",
"node": ">=12.x.x <=16.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}
}

2
server/bootstrap.js vendored
View File

@ -10,7 +10,7 @@ module.exports = ({ strapi }) => {
if (populate && populate[0] === 'deep') {
const depth = populate[1] ?? defaultDepth
const modelObject = getFullPopulateObject(event.model.uid, depth, []);
const modelObject = getFullPopulateObject(event.model.uid, depth);
event.params.populate = modelObject.populate
}
}

View File

@ -1,5 +1,7 @@
const { isEmpty, merge } = require("lodash/fp");
const skipCreatorFields = strapi.plugin('strapi-plugin-populate-deep')?.config('skipCreatorFields') || true;
const getModelPopulationAttributes = (model) => {
if (model.uid === "plugin::upload.file") {
const { related, ...attributes } = model.attributes;
@ -9,9 +11,7 @@ const getModelPopulationAttributes = (model) => {
return model.attributes;
};
const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
const skipCreatorFields = strapi.plugin('strapi-plugin-populate-deep')?.config('skipCreatorFields');
const getFullPopulateObject = (modelUid, maxDepth = 20) => {
if (maxDepth <= 1) {
return true;
}
@ -21,11 +21,9 @@ const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
const populate = {};
const model = strapi.getModel(modelUid);
if (ignore && !ignore.includes(model.collectionName)) ignore.push(model.collectionName)
for (const [key, value] of Object.entries(
getModelPopulationAttributes(model)
)) {
if (ignore?.includes(key)) continue
if (value) {
if (value.type === "component") {
populate[key] = getFullPopulateObject(value.component, maxDepth - 1);
@ -38,8 +36,7 @@ const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
} else if (value.type === "relation") {
const relationPopulate = getFullPopulateObject(
value.target,
(key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1,
ignore
(key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1
);
if (relationPopulate) {
populate[key] = relationPopulate;