strapi-plugin-populate-all 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,6 +8,14 @@ A lightweight Strapi plugin that enables you to **recursively populate** all nes
8
8
  - Fine-tune which relations are populated using plugin configuration.
9
9
  - The generated populate queries are cached, so repeated REST requests for the same model are faster.
10
10
 
11
+ ## Installation
12
+
13
+ All you need to do is install the plugin. Strapi should automatically detect and include it.
14
+
15
+ ```
16
+ npm install strapi-plugin-populate-all
17
+ ```
18
+
11
19
  ## Usage
12
20
 
13
21
  Just add `?populate=all` to your REST API request, for example: `GET /api/articles?populate=all`
@@ -4,12 +4,24 @@ const config = {
4
4
  relations: true
5
5
  },
6
6
  validator(config2) {
7
- const isBoolean = typeof config2?.relations === "boolean";
8
- const isArrayOfStrings = Array.isArray(config2?.relations) && config2?.relations?.every((relation) => typeof relation === "string");
9
- if (!(isBoolean || isArrayOfStrings)) {
10
- throw new Error(
11
- `[populate-all] config "relations" of type ${typeof config2?.relation} is not valid. Supported are boolean or Array of strings.`
12
- );
7
+ for (const key in config2) {
8
+ const value = config2[key];
9
+ switch (key) {
10
+ case "relations": {
11
+ const isBoolean = typeof value === "boolean";
12
+ const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
13
+ if (!(isBoolean || isArrayOfStrings)) {
14
+ throw new Error(
15
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
16
+ );
17
+ }
18
+ break;
19
+ }
20
+ default:
21
+ strapi.log.warn(
22
+ `[populate-all] unknown config "${key}" provided. This config will be ignored.`
23
+ );
24
+ }
13
25
  }
14
26
  }
15
27
  };
@@ -43,6 +55,7 @@ const getPopulateQuery = (modelUid) => {
43
55
  for (const fieldName in model.attributes) {
44
56
  const attribute = model.attributes[fieldName];
45
57
  if (fieldName === "localizations") {
58
+ query.populate[fieldName] = true;
46
59
  continue;
47
60
  }
48
61
  if (attribute.type === "dynamiczone") {
@@ -61,7 +74,7 @@ const getPopulateQuery = (modelUid) => {
61
74
  continue;
62
75
  }
63
76
  const relations = strapi.plugin("populate-all").config("relations");
64
- strapi.log.debug(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
77
+ strapi.log.silly(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
65
78
  if (relations === true) {
66
79
  query.populate[fieldName] = getPopulateQuery(attribute.target);
67
80
  continue;
@@ -72,7 +85,7 @@ const getPopulateQuery = (modelUid) => {
72
85
  }
73
86
  }
74
87
  if (attribute.type === "media") {
75
- query.populate[fieldName] = { populate: true };
88
+ query.populate[fieldName] = true;
76
89
  continue;
77
90
  }
78
91
  }
@@ -94,6 +107,9 @@ const bootstrap = ({ strapi: strapi2 }) => {
94
107
  strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
95
108
  const populateQuery = getPopulateQuery(event.model.uid);
96
109
  if (populateQuery?.populate) {
110
+ strapi2.log.debug(
111
+ `[populate-all] populate query for ${event.model.uid}: ${JSON.stringify(populateQuery.populate)}`
112
+ );
97
113
  event.params.populate = populateQuery.populate;
98
114
  }
99
115
  }
@@ -3,12 +3,24 @@ const config = {
3
3
  relations: true
4
4
  },
5
5
  validator(config2) {
6
- const isBoolean = typeof config2?.relations === "boolean";
7
- const isArrayOfStrings = Array.isArray(config2?.relations) && config2?.relations?.every((relation) => typeof relation === "string");
8
- if (!(isBoolean || isArrayOfStrings)) {
9
- throw new Error(
10
- `[populate-all] config "relations" of type ${typeof config2?.relation} is not valid. Supported are boolean or Array of strings.`
11
- );
6
+ for (const key in config2) {
7
+ const value = config2[key];
8
+ switch (key) {
9
+ case "relations": {
10
+ const isBoolean = typeof value === "boolean";
11
+ const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
12
+ if (!(isBoolean || isArrayOfStrings)) {
13
+ throw new Error(
14
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
15
+ );
16
+ }
17
+ break;
18
+ }
19
+ default:
20
+ strapi.log.warn(
21
+ `[populate-all] unknown config "${key}" provided. This config will be ignored.`
22
+ );
23
+ }
12
24
  }
13
25
  }
14
26
  };
@@ -42,6 +54,7 @@ const getPopulateQuery = (modelUid) => {
42
54
  for (const fieldName in model.attributes) {
43
55
  const attribute = model.attributes[fieldName];
44
56
  if (fieldName === "localizations") {
57
+ query.populate[fieldName] = true;
45
58
  continue;
46
59
  }
47
60
  if (attribute.type === "dynamiczone") {
@@ -60,7 +73,7 @@ const getPopulateQuery = (modelUid) => {
60
73
  continue;
61
74
  }
62
75
  const relations = strapi.plugin("populate-all").config("relations");
63
- strapi.log.debug(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
76
+ strapi.log.silly(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
64
77
  if (relations === true) {
65
78
  query.populate[fieldName] = getPopulateQuery(attribute.target);
66
79
  continue;
@@ -71,7 +84,7 @@ const getPopulateQuery = (modelUid) => {
71
84
  }
72
85
  }
73
86
  if (attribute.type === "media") {
74
- query.populate[fieldName] = { populate: true };
87
+ query.populate[fieldName] = true;
75
88
  continue;
76
89
  }
77
90
  }
@@ -93,6 +106,9 @@ const bootstrap = ({ strapi: strapi2 }) => {
93
106
  strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
94
107
  const populateQuery = getPopulateQuery(event.model.uid);
95
108
  if (populateQuery?.populate) {
109
+ strapi2.log.debug(
110
+ `[populate-all] populate query for ${event.model.uid}: ${JSON.stringify(populateQuery.populate)}`
111
+ );
96
112
  event.params.populate = populateQuery.populate;
97
113
  }
98
114
  }
@@ -2,6 +2,6 @@ declare const _default: {
2
2
  default: {
3
3
  relations: boolean;
4
4
  };
5
- validator(config: any): void;
5
+ validator(config: Record<string, unknown>): void;
6
6
  };
7
7
  export default _default;
@@ -3,7 +3,7 @@ declare const _default: {
3
3
  default: {
4
4
  relations: boolean;
5
5
  };
6
- validator(config: any): void;
6
+ validator(config: Record<string, unknown>): void;
7
7
  };
8
8
  register: ({ strapi }: {
9
9
  strapi: import("@strapi/types/dist/core").Strapi;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.0.2",
2
+ "version": "1.1.0",
3
3
  "name": "strapi-plugin-populate-all",
4
4
  "description": "A lightweight plugin to recursively populate nested data in RESTful API requests",
5
5
  "keywords": [],