strapi-plugin-populate-all 1.1.0 → 1.1.2

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.
@@ -4,24 +4,12 @@ const config = {
4
4
  relations: true
5
5
  },
6
6
  validator(config2) {
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
- }
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
+ );
25
13
  }
26
14
  }
27
15
  };
@@ -52,8 +40,7 @@ const getPopulateQuery = (modelUid) => {
52
40
  }
53
41
  const query = { populate: {} };
54
42
  const model = strapi.getModel(modelUid);
55
- for (const fieldName in model.attributes) {
56
- const attribute = model.attributes[fieldName];
43
+ for (const [fieldName, attribute] of Object.entries(model.attributes)) {
57
44
  if (fieldName === "localizations") {
58
45
  query.populate[fieldName] = true;
59
46
  continue;
@@ -74,7 +61,6 @@ const getPopulateQuery = (modelUid) => {
74
61
  continue;
75
62
  }
76
63
  const relations = strapi.plugin("populate-all").config("relations");
77
- strapi.log.silly(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
78
64
  if (relations === true) {
79
65
  query.populate[fieldName] = getPopulateQuery(attribute.target);
80
66
  continue;
@@ -102,17 +88,21 @@ const getPopulateQuery = (modelUid) => {
102
88
  };
103
89
  const bootstrap = ({ strapi: strapi2 }) => {
104
90
  strapi2.db.lifecycles.subscribe((event) => {
105
- if (event.params?.recursive === "true") {
106
- if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
107
- strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
108
- const populateQuery = getPopulateQuery(event.model.uid);
109
- if (populateQuery?.populate) {
91
+ try {
92
+ if (event.params?.recursive === "true") {
93
+ if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
94
+ strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
95
+ const populateQuery = getPopulateQuery(event.model.uid);
110
96
  strapi2.log.debug(
111
97
  `[populate-all] populate query for ${event.model.uid}: ${JSON.stringify(populateQuery.populate)}`
112
98
  );
113
- event.params.populate = populateQuery.populate;
99
+ if (populateQuery?.populate) {
100
+ event.params.populate = populateQuery.populate;
101
+ }
114
102
  }
115
103
  }
104
+ } catch (error) {
105
+ strapi2.log.error(`[populate-all] failed to apply populate db query: ${error}`);
116
106
  }
117
107
  });
118
108
  };
@@ -3,24 +3,12 @@ const config = {
3
3
  relations: true
4
4
  },
5
5
  validator(config2) {
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
- }
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
+ );
24
12
  }
25
13
  }
26
14
  };
@@ -51,8 +39,7 @@ const getPopulateQuery = (modelUid) => {
51
39
  }
52
40
  const query = { populate: {} };
53
41
  const model = strapi.getModel(modelUid);
54
- for (const fieldName in model.attributes) {
55
- const attribute = model.attributes[fieldName];
42
+ for (const [fieldName, attribute] of Object.entries(model.attributes)) {
56
43
  if (fieldName === "localizations") {
57
44
  query.populate[fieldName] = true;
58
45
  continue;
@@ -73,7 +60,6 @@ const getPopulateQuery = (modelUid) => {
73
60
  continue;
74
61
  }
75
62
  const relations = strapi.plugin("populate-all").config("relations");
76
- strapi.log.silly(`[populate-all] relations to populate ${JSON.stringify(relations)}`);
77
63
  if (relations === true) {
78
64
  query.populate[fieldName] = getPopulateQuery(attribute.target);
79
65
  continue;
@@ -101,17 +87,21 @@ const getPopulateQuery = (modelUid) => {
101
87
  };
102
88
  const bootstrap = ({ strapi: strapi2 }) => {
103
89
  strapi2.db.lifecycles.subscribe((event) => {
104
- if (event.params?.recursive === "true") {
105
- if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
106
- strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
107
- const populateQuery = getPopulateQuery(event.model.uid);
108
- if (populateQuery?.populate) {
90
+ try {
91
+ if (event.params?.recursive === "true") {
92
+ if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
93
+ strapi2.log.debug(`[populate-all] recursively populate ${event.model.uid}`);
94
+ const populateQuery = getPopulateQuery(event.model.uid);
109
95
  strapi2.log.debug(
110
96
  `[populate-all] populate query for ${event.model.uid}: ${JSON.stringify(populateQuery.populate)}`
111
97
  );
112
- event.params.populate = populateQuery.populate;
98
+ if (populateQuery?.populate) {
99
+ event.params.populate = populateQuery.populate;
100
+ }
113
101
  }
114
102
  }
103
+ } catch (error) {
104
+ strapi2.log.error(`[populate-all] failed to apply populate db query: ${error}`);
115
105
  }
116
106
  });
117
107
  };
@@ -2,6 +2,6 @@ declare const _default: {
2
2
  default: {
3
3
  relations: boolean;
4
4
  };
5
- validator(config: Record<string, unknown>): void;
5
+ validator(config: any): 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: Record<string, unknown>): void;
6
+ validator(config: any): 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.1.0",
2
+ "version": "1.1.2",
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": [],