npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-deep-populate

v1.0.32

Published

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

Downloads

10

Readme

Strapi plugin custom-deep-populate

This Strapi 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 of these features is fully customizable to suit your specific needs.


Performance Improvements

With recent optimizations, this plugin delivers even faster performance than similar plugins such as strapi-plugin-populate-deep.

  • Request Time Reduction: The time taken for a request has been reduced from 210ms to 180ms (average) when using the same content manager, resulting in a 14.29% decrease in request time.
  • Data Size Reduction: The size of the returned information has been minimized from 8KB to 3KB (average), indicating a 62.5% reduction in data size.
  • File Complexity Reduction: The complexity of reading files has been significantly reduced, with the average number of lines decreasing from 500 to 200, resulting in a 60% reduction in file complexity.

| Improvement Type | strapi-plugin-populate-deep | custom-deep-populate | Percentage Improvement (average) | |--------------------------|-----------------------------|----------------------|----------------------------------| | Request Time Reduction | 210ms | 180ms | 14.29% | | Data Size Reduction | 8KB | 3KB | 62.5% | | File Complexity Reduction| 500 lines | 200 lines | 60% |

These results were obtained by comparing the performance of the strapi-plugin-populate-deep plugin with the custom-deep-populate plugin with the next configurations:

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

npm i custom-deep-populate

Or

yarn add custom-deep-populate

Or

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,<int>

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,<int>,<string>,<string>,...,<string>

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

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

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.

Contributions

The original idea for getting the populated structure was created by tomnovotny7 and can be found in this github thread, then was created a plugin by Barelydead (Christofer Jungberg) and can be found here. From this plugin, the current plugin was created.