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.
- package/dist/server/index.js +17 -27
- package/dist/server/index.mjs +17 -27
- package/dist/server/src/config/index.d.ts +1 -1
- package/dist/server/src/index.d.ts +1 -1
- package/package.json +1 -1
package/dist/server/index.js
CHANGED
|
@@ -4,24 +4,12 @@ const config = {
|
|
|
4
4
|
relations: true
|
|
5
5
|
},
|
|
6
6
|
validator(config2) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
|
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
|
-
|
|
106
|
-
if (event.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
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
|
};
|
package/dist/server/index.mjs
CHANGED
|
@@ -3,24 +3,12 @@ const config = {
|
|
|
3
3
|
relations: true
|
|
4
4
|
},
|
|
5
5
|
validator(config2) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
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
|
-
|
|
105
|
-
if (event.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
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
|
};
|
package/package.json
CHANGED