-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christofer Jungberg
committed
Feb 2, 2022
0 parents
commit 01b30fe
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
default: {}, | ||
validator() {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const bootstrap = require('./bootstrap'); | ||
|
||
module.exports = { | ||
bootstrap, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
'use strict'; | ||
|
||
module.exports = require('./server'); |