@strapi/utils 4.5.2 → 4.5.3

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.
@@ -104,12 +104,16 @@ const isPrivateAttribute = (model = {}, attributeName) => {
104
104
  };
105
105
 
106
106
  const isScalarAttribute = (attribute) => {
107
- return !['media', 'component', 'relation', 'dynamiczone'].includes(attribute.type);
107
+ return !['media', 'component', 'relation', 'dynamiczone'].includes(attribute?.type);
108
+ };
109
+ const isMediaAttribute = (attribute) => attribute?.type === 'media';
110
+ const isRelationalAttribute = (attribute) => attribute?.type === 'relation';
111
+ const isComponentAttribute = (attribute) => ['component', 'dynamiczone'].includes(attribute?.type);
112
+
113
+ const isDynamicZoneAttribute = (attribute) => attribute?.type === 'dynamiczone';
114
+ const isMorphToRelationalAttribute = (attribute) => {
115
+ return isRelationalAttribute(attribute) && attribute?.relation?.startsWith?.('morphTo');
108
116
  };
109
- const isMediaAttribute = (attribute) => attribute && attribute.type === 'media';
110
- const isRelationalAttribute = (attribute) => attribute && attribute.type === 'relation';
111
- const isComponentAttribute = (attribute) =>
112
- attribute && ['component', 'dynamiczone'].includes(attribute.type);
113
117
 
114
118
  const getComponentAttributes = (schema) => {
115
119
  return _.reduce(
@@ -158,6 +162,8 @@ module.exports = {
158
162
  isMediaAttribute,
159
163
  isRelationalAttribute,
160
164
  isComponentAttribute,
165
+ isDynamicZoneAttribute,
166
+ isMorphToRelationalAttribute,
161
167
  isTypedAttribute,
162
168
  getPrivateAttributes,
163
169
  isPrivateAttribute,
@@ -6,7 +6,11 @@
6
6
  * Converts the standard Strapi REST query params to a more usable format for querying
7
7
  * You can read more here: https://docs.strapi.io/developer-docs/latest/developer-resources/database-apis-reference/rest-api.html#filters
8
8
  */
9
+
9
10
  const {
11
+ isNil,
12
+ toNumber,
13
+ isInteger,
10
14
  has,
11
15
  isEmpty,
12
16
  isObject,
@@ -14,14 +18,16 @@ const {
14
18
  cloneDeep,
15
19
  get,
16
20
  mergeAll,
17
- isNil,
18
- toNumber,
19
- isInteger,
20
21
  } = require('lodash/fp');
21
22
  const _ = require('lodash');
22
23
  const parseType = require('./parse-type');
23
24
  const contentTypesUtils = require('./content-types');
24
25
  const { PaginationError } = require('./errors');
26
+ const {
27
+ isMediaAttribute,
28
+ isDynamicZoneAttribute,
29
+ isMorphToRelationalAttribute,
30
+ } = require('./content-types');
25
31
 
26
32
  const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
27
33
 
@@ -185,8 +191,31 @@ const convertPopulateObject = (populate, schema) => {
185
191
  return acc;
186
192
  }
187
193
 
188
- // FIXME: This is a temporary solution for dynamic zones that should be
189
- // fixed when we'll implement a more accurate way to query them
194
+ // Allow adding an 'on' strategy to populate queries for polymorphic relations, media and dynamic zones
195
+ const isAllowedAttributeForFragmentPopulate =
196
+ isDynamicZoneAttribute(attribute) ||
197
+ isMediaAttribute(attribute) ||
198
+ isMorphToRelationalAttribute(attribute);
199
+
200
+ const hasFragmentPopulateDefined = typeof subPopulate === 'object' && 'on' in subPopulate;
201
+
202
+ if (isAllowedAttributeForFragmentPopulate && hasFragmentPopulateDefined) {
203
+ return {
204
+ ...acc,
205
+ [key]: {
206
+ on: Object.entries(subPopulate.on).reduce(
207
+ (acc, [type, typeSubPopulate]) => ({
208
+ ...acc,
209
+ [type]: convertNestedPopulate(typeSubPopulate, strapi.getModel(type)),
210
+ }),
211
+ {}
212
+ ),
213
+ },
214
+ };
215
+ }
216
+
217
+ // TODO: This is a query's populate fallback for DynamicZone and is kept for legacy purpose.
218
+ // Removing it could break existing user queries but it should be removed in V5.
190
219
  if (attribute.type === 'dynamiczone') {
191
220
  const populates = attribute.components
192
221
  .map((uid) => strapi.getModel(uid))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/utils",
3
- "version": "4.5.2",
3
+ "version": "4.5.3",
4
4
  "description": "Shared utilities for the Strapi packages",
5
5
  "keywords": [
6
6
  "strapi",
@@ -45,5 +45,5 @@
45
45
  "node": ">=14.19.1 <=18.x.x",
46
46
  "npm": ">=6.0.0"
47
47
  },
48
- "gitHead": "bcb1b7f472aae2556a9b59d59ee66d241c497a3e"
48
+ "gitHead": "5453885f7aa329f98d047a6030a8e98ba3cbdb41"
49
49
  }