mirror of
https://github.com/Barelydead/strapi-plugin-populate-deep.git
synced 2025-06-23 00:00:01 -04:00
Compare commits
No commits in common. "main" and "v1.1.0" have entirely different histories.
@ -32,7 +32,7 @@ The populate deep option is available for all collections and single types using
|
|||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
|
|
||||||
The default depth can be customized via the plugin config. To do so create or edit you plugins.js file.
|
The default depth and behaviour for populating creator fields can be customized via the plugin config. To do so create or edit you plugins.js file.
|
||||||
|
|
||||||
## Example configuration
|
## Example configuration
|
||||||
|
|
||||||
@ -40,9 +40,11 @@ The default depth can be customized via the plugin config. To do so create or ed
|
|||||||
|
|
||||||
```
|
```
|
||||||
module.exports = ({ env }) => ({
|
module.exports = ({ env }) => ({
|
||||||
'strapi-plugin-populate-deep': {
|
'populate-deep': {
|
||||||
|
enabled: true,
|
||||||
config: {
|
config: {
|
||||||
defaultDepth: 3, // Default is 5
|
defaultDepth: 3, // Default is 5
|
||||||
|
skipCreatorFields: false, // Default is true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "strapi-plugin-populate-deep",
|
"name": "strapi-plugin-populate-deep",
|
||||||
"version": "3.0.1",
|
"version": "1.1.0",
|
||||||
"lockfileVersion": 1
|
"lockfileVersion": 1
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "strapi-plugin-populate-deep",
|
"name": "strapi-plugin-populate-deep",
|
||||||
"version": "3.0.1",
|
"version": "1.1.0",
|
||||||
"description": "Strapi plugin that populates nested content.",
|
"description": "This is the description of the plugin.",
|
||||||
"strapi": {
|
"strapi": {
|
||||||
"name": "strapi-plugin-populate-deep",
|
"name": "strapi-plugin-populate-deep",
|
||||||
"description": "Api helper to populate deep content structures.",
|
"description": "Api helper to populate deep content structures.",
|
||||||
@ -21,8 +21,8 @@
|
|||||||
"url": "https://github.com/Barelydead/strapi-plugin-deep-populate"
|
"url": "https://github.com/Barelydead/strapi-plugin-deep-populate"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=14.19.1 <=20.x.x",
|
"node": ">=12.x.x <=16.x.x",
|
||||||
"npm": ">=6.0.0"
|
"npm": ">=6.0.0"
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
4
server/bootstrap.js
vendored
4
server/bootstrap.js
vendored
@ -6,11 +6,11 @@ module.exports = ({ strapi }) => {
|
|||||||
strapi.db.lifecycles.subscribe((event) => {
|
strapi.db.lifecycles.subscribe((event) => {
|
||||||
if (event.action === 'beforeFindMany' || event.action === 'beforeFindOne') {
|
if (event.action === 'beforeFindMany' || event.action === 'beforeFindOne') {
|
||||||
const populate = event.params?.populate;
|
const populate = event.params?.populate;
|
||||||
const defaultDepth = strapi.plugin('strapi-plugin-populate-deep')?.config('defaultDepth') || 5
|
const defaultDepth = strapi.plugin('populate-deep')?.config('defaultDepth') || 5
|
||||||
|
|
||||||
if (populate && populate[0] === 'deep') {
|
if (populate && populate[0] === 'deep') {
|
||||||
const depth = populate[1] ?? defaultDepth
|
const depth = populate[1] ?? defaultDepth
|
||||||
const modelObject = getFullPopulateObject(event.model.uid, depth, []);
|
const modelObject = getFullPopulateObject(event.model.uid, depth);
|
||||||
event.params.populate = modelObject.populate
|
event.params.populate = modelObject.populate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
const { isEmpty, merge } = require("lodash/fp");
|
const { isEmpty, merge } = require("lodash/fp");
|
||||||
|
|
||||||
|
const skipCreatorFields = strapi.plugin('populate-deep')?.config('skipCreatorFields') || true;
|
||||||
|
|
||||||
const getModelPopulationAttributes = (model) => {
|
const getModelPopulationAttributes = (model) => {
|
||||||
if (model.uid === "plugin::upload.file") {
|
if (model.uid === "plugin::upload.file") {
|
||||||
const { related, ...attributes } = model.attributes;
|
const { related, ...attributes } = model.attributes;
|
||||||
@ -9,9 +11,7 @@ const getModelPopulationAttributes = (model) => {
|
|||||||
return model.attributes;
|
return model.attributes;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
|
const getFullPopulateObject = (modelUid, maxDepth = 20) => {
|
||||||
const skipCreatorFields = strapi.plugin('strapi-plugin-populate-deep')?.config('skipCreatorFields');
|
|
||||||
|
|
||||||
if (maxDepth <= 1) {
|
if (maxDepth <= 1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -21,11 +21,9 @@ const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
|
|||||||
|
|
||||||
const populate = {};
|
const populate = {};
|
||||||
const model = strapi.getModel(modelUid);
|
const model = strapi.getModel(modelUid);
|
||||||
if (ignore && !ignore.includes(model.collectionName)) ignore.push(model.collectionName)
|
|
||||||
for (const [key, value] of Object.entries(
|
for (const [key, value] of Object.entries(
|
||||||
getModelPopulationAttributes(model)
|
getModelPopulationAttributes(model)
|
||||||
)) {
|
)) {
|
||||||
if (ignore?.includes(key)) continue
|
|
||||||
if (value) {
|
if (value) {
|
||||||
if (value.type === "component") {
|
if (value.type === "component") {
|
||||||
populate[key] = getFullPopulateObject(value.component, maxDepth - 1);
|
populate[key] = getFullPopulateObject(value.component, maxDepth - 1);
|
||||||
@ -38,8 +36,7 @@ const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => {
|
|||||||
} else if (value.type === "relation") {
|
} else if (value.type === "relation") {
|
||||||
const relationPopulate = getFullPopulateObject(
|
const relationPopulate = getFullPopulateObject(
|
||||||
value.target,
|
value.target,
|
||||||
(key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1,
|
(key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1
|
||||||
ignore
|
|
||||||
);
|
);
|
||||||
if (relationPopulate) {
|
if (relationPopulate) {
|
||||||
populate[key] = relationPopulate;
|
populate[key] = relationPopulate;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user