This plugin streamlines the process of populating complex content structures through the REST API. It enables you to specify a custom depth for data retrieval, eliminate unwanted fields, and filter out nested fields with identical names. Best of all, each
npm install custom-deep-populatestrapi-plugin-populate-deep.
strapi-plugin-populate-deep plugin with the custom-deep-populate plugin with the next configurations:
js
module.exports = ({ env }) => ({
'custom-deep-populate': {
config: {
unnecessaryFields: ["createdAt", "updatedAt", "publishedAt", "createdBy", "updatedBy", "id"],
fieldsToKeepInImage: ["url", "alternativeText"],
removeNestedFieldsWithSameName: true,
defaultDepth: 10,
}
},
});
`
These enhancements ensure that your Strapi experience is not only more efficient but also more responsive, enhancing overall user satisfaction.
---
Installation
`sh
npm i custom-deep-populate
`
Or
`sh
yarn add custom-deep-populate
`
Or
`sh
pnpm i custom-deep-populate
`
---
Usages
Examples
Populate a request with the default max depth.
`
/api/articles?populate=custom
`
Populate a request with a custom depth
`
/api/articles?populate=custom,
`
For example, to populate a request with a custom depth of 5:
`
/api/articles?populate=custom,5
`
Or
`
/api/articles/1?populate=custom,5
`
Populate a request with a custom depth and remove fields from unnecessary fields
(in config.js/config.ts) using the remove keyword.
`
/api/articles?populate=custom,,,,...,
`
For example, to populate a request with a custom depth of 5 and remove the unnecessary fields createdAt and updatedAt:
`
/api/articles?populate=custom,5,createdAt,updatedAt
`
Good to know
The default max depth is 10 levels deep.
The populate deep option is available for all collections and single types using the findOne and findMany methods.
---
Configuration
The default values can be customized via the plugin config. To do it, create or edit your plugins.js/plugins.ts file.
Example configuration
config/plugins.js
`js
module.exports = ({ env }) => ({
'custom-deep-populate': {
config: {
unnecessaryFields: [], // OPTIONAL -> Default: ["createdAt", "updatedAt", "publishedAt", "createdBy", "updatedBy"]
fieldsToKeepInImage: [], // OPTIONAL -> Default: ["url", "alternativeText"]
removeNestedFieldsWithSameName: true, // OPTIONAL -> Default: true
imageFormats: false, // OPTIONAL -> Default: false (requires fieldsToKeepInImage to be set with the value "url" in the array in config.js/config.ts)
defaultDepth: 10, // OPTIONAL -> Default: 10
}
},
});
`
Or
config/plugins.ts
`ts
export default {
'custom-deep-populate': {
config: {
unnecessaryFields: [], // OPTIONAL -> Default: ["createdAt", "updatedAt", "publishedAt", "createdBy", "updatedBy"]
fieldsToKeepInImage: [], // OPTIONAL -> Default: ["url", "alternativeText"]
removeNestedFieldsWithSameName: true, // OPTIONAL -> Default: true
imageFormats: false, // OPTIONAL -> Default: false (requires fieldsToKeepInImage to be set with the value "url" in the array in config.js/config.ts)
defaultDepth: 10, // OPTIONAL -> Default: 10
}
},
};
`
Configuration options extended
1. unnecessaryFields - An array of fields that should be removed from the response. When the array is empty, the response will contain all fields.
2. fieldsToKeepInImage - An array of fields that should be kept in the image response. When the array is empty, the response will contain only the url and alternativeText fields.
3. removeNestedFieldsWithSameName - A boolean that determines whether nested fields with the same name should be removed from the response. When set to true, the response will contain only the first field with the same name. F.E. if there is a field nestes as {icon: {icon: "iconName"}} where icon is the same name, the response will contain only the first icon field as {icon: "iconName"}. _NOTE: Used usually when has collections with the same name as the nested field._
4. imageFormats - A boolean that allow to get the image formats in the image response. When set to true, the response will contain the formats of the image. F.E. Will contain the urls of the image in the different available formats in the current image (thumbnail, small, medium, large, etc). _NOTE: Requires fieldsToKeepInImage to be set with the value "url" in the array in config.js/config.ts._
- NOTE: To get the image formats in the image response in a specific endpoint, can use imageFormats directly in the query string as:
`
api/articles?populate=custom,10,...,imageFormats,...
`
5. defaultDepth` - An integer that determines the default depth for the populate deep option. When set to 10, the response will contain data up to 10 levels deep.