Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Christofer Jungberg committed Feb 2, 2022
0 parents commit 01b30fe
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Strapi plugin populate-deep
This plugin allows for easier population of deep content structures using the rest API.

The original idea for getting the populate structure was created by [tomnovotny7](https://github.com/tomnovotny7) and can be found in [this](https://github.com/strapi/strapi/issues/11836) github thread


# Examples

Populate a request with the default max depth.
`/api/articles?populate=deep`

Populate a request with the a custom depth
`/api/articles?populate=deep,10`

# Installation

`npm install strapi-plugin-deep-populate`

`yarn add strapi-plugin-deep-populate`
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "populate-deep",
"version": "0.1.0",
"description": "This is the description of the plugin.",
"strapi": {
"name": "Populate deep",
"description": "Api helper to populate deep content structures.",
"kind": "plugin"
},
"dependencies": {},
"author": {
"name": "Christofer Jungberg"
},
"maintainers": [
{
"name": "Christofer Jungberg"
}
],
"engines": {
"node": ">=12.x. <=16.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}
17 changes: 17 additions & 0 deletions server/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const { getFullPopulateObject } = require('./helpers')

module.exports = ({ strapi }) => {
// Subscribe to the lifecycles that we are intrested in.
strapi.db.lifecycles.subscribe((event) => {
if (event.action === 'beforeFindMany' || event.action === 'beforeFindOne') {
const populate = event.params?.populate;

if (populate && populate[0] === 'deep') {
const depth = populate[1] ?? 5
const modelObject = getFullPopulateObject(event.model.uid, depth);
event.params.populate = modelObject.populate
}
}
});
};
6 changes: 6 additions & 0 deletions server/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = {
default: {},
validator() {},
};
48 changes: 48 additions & 0 deletions server/helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const { isEmpty, merge } = require("lodash/fp");

const getModelPopulationAttributes = (model) => {
if (model.uid === "plugin::upload.file") {
const { related, ...attributes } = model.attributes;
return attributes;
}

return model.attributes;
};

const getFullPopulateObject = (modelUid, maxDepth = 20) => {
if (maxDepth <= 1) {
return true;
}
if (modelUid === "admin::user") {
return undefined;
}

const populate = {};
const model = strapi.getModel(modelUid);
for (const [key, value] of Object.entries(
getModelPopulationAttributes(model)
)) {
if (value) {
if (value.type === "component") {
populate[key] = getFullPopulateObject(value.component, maxDepth - 1);
} else if (value.type === "dynamiczone") {
const dynamicPopulate = value.components.reduce((prev, cur) => {
const curPopulate = getFullPopulateObject(cur, maxDepth - 1);
return curPopulate === true ? prev : merge(prev, curPopulate);
}, {});
populate[key] = isEmpty(dynamicPopulate) ? true : dynamicPopulate;
} else if (value.type === "relation") {
const relationPopulate = getFullPopulateObject(
value.target,
maxDepth - 1
);
if (relationPopulate) {
populate[key] = relationPopulate;
}
} else if (value.type === "media") {
populate[key] = true;
}
}
}
return isEmpty(populate) ? true : { populate };
};
7 changes: 7 additions & 0 deletions server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

const bootstrap = require('./bootstrap');

module.exports = {
bootstrap,
};
3 changes: 3 additions & 0 deletions strapi-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('./server');

0 comments on commit 01b30fe

Please sign in to comment.