@webiny/api-headless-cms 5.37.8 → 5.38.0-beta.1

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.
Files changed (308) hide show
  1. package/constants.js +3 -1
  2. package/context.js +10 -17
  3. package/context.js.map +1 -1
  4. package/crud/contentEntry/afterDelete.js +3 -1
  5. package/crud/contentEntry/beforeCreate.js +3 -1
  6. package/crud/contentEntry/beforeUpdate.js +3 -1
  7. package/crud/contentEntry/entryDataValidation.d.ts +4 -2
  8. package/crud/contentEntry/entryDataValidation.js +220 -47
  9. package/crud/contentEntry/entryDataValidation.js.map +1 -1
  10. package/crud/contentEntry/markLockedFields.js +3 -1
  11. package/crud/contentEntry/referenceFieldsMapping.d.ts +6 -0
  12. package/crud/contentEntry/referenceFieldsMapping.js +118 -97
  13. package/crud/contentEntry/referenceFieldsMapping.js.map +1 -1
  14. package/crud/contentEntry/searchableFields.js +5 -3
  15. package/crud/contentEntry/searchableFields.js.map +1 -1
  16. package/crud/contentEntry.crud.js +68 -64
  17. package/crud/contentEntry.crud.js.map +1 -1
  18. package/crud/contentModel/beforeCreate.d.ts +1 -1
  19. package/crud/contentModel/beforeCreate.js +11 -9
  20. package/crud/contentModel/beforeCreate.js.map +1 -1
  21. package/crud/contentModel/beforeDelete.js +3 -1
  22. package/crud/contentModel/beforeUpdate.d.ts +1 -1
  23. package/crud/contentModel/beforeUpdate.js +3 -6
  24. package/crud/contentModel/beforeUpdate.js.map +1 -1
  25. package/crud/contentModel/compatibility/modelApiName.js +3 -1
  26. package/crud/contentModel/contentModelManagerFactory.js +3 -1
  27. package/crud/contentModel/createFieldStorageId.js +3 -1
  28. package/crud/contentModel/defaultFields.js +3 -1
  29. package/crud/contentModel/ensureTypeTag.d.ts +5 -0
  30. package/crud/contentModel/ensureTypeTag.js +21 -0
  31. package/crud/contentModel/ensureTypeTag.js.map +1 -0
  32. package/crud/contentModel/fields/descriptionField.js +3 -1
  33. package/crud/contentModel/fields/imageField.js +3 -1
  34. package/crud/contentModel/fields/titleField.js +3 -1
  35. package/crud/contentModel/listModelsFromDatabase.d.ts +10 -0
  36. package/crud/contentModel/listModelsFromDatabase.js +39 -0
  37. package/crud/contentModel/listModelsFromDatabase.js.map +1 -0
  38. package/crud/contentModel/validate/endingAllowed.js +3 -1
  39. package/crud/contentModel/validate/isModelEndingAllowed.js +4 -2
  40. package/crud/contentModel/validate/isModelEndingAllowed.js.map +1 -1
  41. package/crud/contentModel/validate/modelId.js +3 -1
  42. package/crud/contentModel/validate/pluralApiName.js +3 -1
  43. package/crud/contentModel/validate/singularApiName.js +3 -1
  44. package/crud/contentModel/validateModel.js +3 -1
  45. package/crud/contentModel/validateModel.js.map +1 -1
  46. package/crud/contentModel/validateModelFields.js +3 -1
  47. package/crud/contentModel/validation.d.ts +339 -58
  48. package/crud/contentModel/validation.js +71 -11
  49. package/crud/contentModel/validation.js.map +1 -1
  50. package/crud/contentModel.crud.js +80 -156
  51. package/crud/contentModel.crud.js.map +1 -1
  52. package/crud/contentModelGroup/beforeCreate.d.ts +1 -1
  53. package/crud/contentModelGroup/beforeCreate.js +15 -1
  54. package/crud/contentModelGroup/beforeCreate.js.map +1 -1
  55. package/crud/contentModelGroup/beforeDelete.js +3 -1
  56. package/crud/contentModelGroup/beforeUpdate.js +3 -1
  57. package/crud/contentModelGroup/listGroupsFromDatabase.d.ts +8 -0
  58. package/crud/contentModelGroup/listGroupsFromDatabase.js +22 -0
  59. package/crud/contentModelGroup/listGroupsFromDatabase.js.map +1 -0
  60. package/crud/contentModelGroup/validation.d.ts +9 -6
  61. package/crud/contentModelGroup/validation.js +5 -2
  62. package/crud/contentModelGroup/validation.js.map +1 -1
  63. package/crud/contentModelGroup.crud.js +89 -110
  64. package/crud/contentModelGroup.crud.js.map +1 -1
  65. package/crud/system.crud.js +4 -7
  66. package/crud/system.crud.js.map +1 -1
  67. package/export/crud/exporting.d.ts +3 -0
  68. package/export/crud/exporting.js +50 -0
  69. package/export/crud/exporting.js.map +1 -0
  70. package/export/crud/importing.d.ts +3 -0
  71. package/export/crud/importing.js +79 -0
  72. package/export/crud/importing.js.map +1 -0
  73. package/export/crud/imports/importData.d.ts +14 -0
  74. package/export/crud/imports/importData.js +25 -0
  75. package/export/crud/imports/importData.js.map +1 -0
  76. package/export/crud/imports/importGroups.d.ts +8 -0
  77. package/export/crud/imports/importGroups.js +99 -0
  78. package/export/crud/imports/importGroups.js.map +1 -0
  79. package/export/crud/imports/importModels.d.ts +8 -0
  80. package/export/crud/imports/importModels.js +134 -0
  81. package/export/crud/imports/importModels.js.map +1 -0
  82. package/export/crud/imports/validateGroups.d.ts +8 -0
  83. package/export/crud/imports/validateGroups.js +106 -0
  84. package/export/crud/imports/validateGroups.js.map +1 -0
  85. package/export/crud/imports/validateInput.d.ts +19 -0
  86. package/export/crud/imports/validateInput.js +49 -0
  87. package/export/crud/imports/validateInput.js.map +1 -0
  88. package/export/crud/imports/validateModels.d.ts +9 -0
  89. package/export/crud/imports/validateModels.js +204 -0
  90. package/export/crud/imports/validateModels.js.map +1 -0
  91. package/export/crud/index.d.ts +4 -0
  92. package/export/crud/index.js +15 -0
  93. package/export/crud/index.js.map +1 -0
  94. package/export/crud/sanitize.d.ts +4 -0
  95. package/export/crud/sanitize.js +35 -0
  96. package/export/crud/sanitize.js.map +1 -0
  97. package/export/graphql/index.d.ts +3 -0
  98. package/export/graphql/index.js +188 -0
  99. package/export/graphql/index.js.map +1 -0
  100. package/export/index.d.ts +2 -0
  101. package/export/index.js +29 -0
  102. package/export/index.js.map +1 -0
  103. package/export/types.d.ts +111 -0
  104. package/export/types.js +22 -0
  105. package/export/types.js.map +1 -0
  106. package/fieldConverters/CmsModelDefaultFieldConverterPlugin.js +3 -1
  107. package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js +3 -1
  108. package/fieldConverters/CmsModelObjectFieldConverterPlugin.js +3 -1
  109. package/fieldConverters/index.js +3 -1
  110. package/graphql/buildSchemaPlugins.js +3 -1
  111. package/graphql/checkEndpointAccess.js +3 -1
  112. package/graphql/createExecutableSchema.js +3 -1
  113. package/graphql/createRequestBody.js +3 -1
  114. package/graphql/formatErrorPayload.js +3 -1
  115. package/graphql/generateSchema.js +3 -1
  116. package/graphql/getSchema.d.ts +0 -1
  117. package/graphql/getSchema.js +23 -13
  118. package/graphql/getSchema.js.map +1 -1
  119. package/graphql/graphQLHandlerFactory.js +3 -1
  120. package/graphql/handleRequest.js +3 -5
  121. package/graphql/handleRequest.js.map +1 -1
  122. package/graphql/index.d.ts +1 -1
  123. package/graphql/index.js +4 -2
  124. package/graphql/index.js.map +1 -1
  125. package/graphql/schema/baseContentSchema.js +3 -1
  126. package/graphql/schema/baseSchema.d.ts +2 -3
  127. package/graphql/schema/baseSchema.js +58 -2
  128. package/graphql/schema/baseSchema.js.map +1 -1
  129. package/graphql/schema/contentEntries.js +4 -2
  130. package/graphql/schema/contentEntries.js.map +1 -1
  131. package/graphql/schema/contentModelGroups.js +4 -1
  132. package/graphql/schema/contentModelGroups.js.map +1 -1
  133. package/graphql/schema/contentModels.js +5 -5
  134. package/graphql/schema/contentModels.js.map +1 -1
  135. package/graphql/schema/createFieldResolvers.js +3 -1
  136. package/graphql/schema/createFieldTypePluginRecords.js +3 -1
  137. package/graphql/schema/createManageResolvers.js +7 -1
  138. package/graphql/schema/createManageResolvers.js.map +1 -1
  139. package/graphql/schema/createManageSDL.js +8 -4
  140. package/graphql/schema/createManageSDL.js.map +1 -1
  141. package/graphql/schema/createPreviewResolvers.js +3 -1
  142. package/graphql/schema/createReadResolvers.js +3 -1
  143. package/graphql/schema/createReadSDL.js +3 -1
  144. package/graphql/schema/resolvers/commonFieldResolvers.js +3 -1
  145. package/graphql/schema/resolvers/manage/resolveCreate.d.ts +2 -1
  146. package/graphql/schema/resolvers/manage/resolveCreate.js +4 -2
  147. package/graphql/schema/resolvers/manage/resolveCreate.js.map +1 -1
  148. package/graphql/schema/resolvers/manage/resolveCreateFrom.d.ts +2 -1
  149. package/graphql/schema/resolvers/manage/resolveCreateFrom.js +4 -2
  150. package/graphql/schema/resolvers/manage/resolveCreateFrom.js.map +1 -1
  151. package/graphql/schema/resolvers/manage/resolveDelete.js +3 -1
  152. package/graphql/schema/resolvers/manage/resolveDeleteMultiple.js +3 -1
  153. package/graphql/schema/resolvers/manage/resolveGet.js +3 -1
  154. package/graphql/schema/resolvers/manage/resolveGetByIds.js +3 -1
  155. package/graphql/schema/resolvers/manage/resolveGetRevisions.js +3 -1
  156. package/graphql/schema/resolvers/manage/resolveGetUniqueFieldValues.js +3 -1
  157. package/graphql/schema/resolvers/manage/resolveList.js +3 -1
  158. package/graphql/schema/resolvers/manage/resolveMove.js +3 -1
  159. package/graphql/schema/resolvers/manage/resolvePublish.js +3 -1
  160. package/graphql/schema/resolvers/manage/resolveRepublish.js +3 -1
  161. package/graphql/schema/resolvers/manage/resolveUnpublish.js +3 -1
  162. package/graphql/schema/resolvers/manage/resolveUpdate.d.ts +2 -1
  163. package/graphql/schema/resolvers/manage/resolveUpdate.js +4 -2
  164. package/graphql/schema/resolvers/manage/resolveUpdate.js.map +1 -1
  165. package/graphql/schema/resolvers/manage/resolveValidate.d.ts +8 -0
  166. package/graphql/schema/resolvers/manage/resolveValidate.js +20 -0
  167. package/graphql/schema/resolvers/manage/resolveValidate.js.map +1 -0
  168. package/graphql/schema/resolvers/preview/resolveGet.js +3 -1
  169. package/graphql/schema/resolvers/preview/resolveList.js +3 -1
  170. package/graphql/schema/resolvers/read/resolveGet.js +3 -1
  171. package/graphql/schema/resolvers/read/resolveList.js +3 -1
  172. package/graphql/schema/schemaPlugins.js +3 -1
  173. package/graphql/system.js +3 -1
  174. package/graphqlFields/boolean.js +3 -1
  175. package/graphqlFields/datetime.js +4 -3
  176. package/graphqlFields/datetime.js.map +1 -1
  177. package/graphqlFields/dynamicZone/dynamicZoneField.js +29 -6
  178. package/graphqlFields/dynamicZone/dynamicZoneField.js.map +1 -1
  179. package/graphqlFields/dynamicZone/dynamicZoneStorage.js +3 -1
  180. package/graphqlFields/dynamicZone/index.js +3 -1
  181. package/graphqlFields/file.js +3 -1
  182. package/graphqlFields/helpers.d.ts +0 -1
  183. package/graphqlFields/helpers.js +6 -18
  184. package/graphqlFields/helpers.js.map +1 -1
  185. package/graphqlFields/index.js +3 -1
  186. package/graphqlFields/longText.js +14 -3
  187. package/graphqlFields/longText.js.map +1 -1
  188. package/graphqlFields/number.js +3 -1
  189. package/graphqlFields/object.js +5 -17
  190. package/graphqlFields/object.js.map +1 -1
  191. package/graphqlFields/ref.js +4 -2
  192. package/graphqlFields/ref.js.map +1 -1
  193. package/graphqlFields/richText.js +3 -1
  194. package/graphqlFields/text.js +3 -1
  195. package/index.d.ts +1 -1
  196. package/index.js +5 -2
  197. package/index.js.map +1 -1
  198. package/modelManager/DefaultCmsModelManager.js +3 -1
  199. package/modelManager/index.js +3 -1
  200. package/package.json +18 -19
  201. package/parameters/context.js +3 -1
  202. package/parameters/header.js +3 -1
  203. package/parameters/index.js +3 -1
  204. package/parameters/manual.js +3 -1
  205. package/parameters/path.js +3 -1
  206. package/plugins/CmsGraphQLSchemaPlugin.js +3 -1
  207. package/plugins/CmsGraphQLSchemaSorterPlugin.js +3 -1
  208. package/plugins/CmsGroupPlugin.d.ts +6 -3
  209. package/plugins/CmsGroupPlugin.js +7 -2
  210. package/plugins/CmsGroupPlugin.js.map +1 -1
  211. package/plugins/CmsModelFieldConverterPlugin.js +3 -1
  212. package/plugins/CmsModelPlugin.d.ts +2 -2
  213. package/plugins/CmsModelPlugin.js +6 -2
  214. package/plugins/CmsModelPlugin.js.map +1 -1
  215. package/plugins/CmsParametersPlugin.js +3 -1
  216. package/plugins/StorageOperationsCmsModelPlugin.js +3 -1
  217. package/plugins/StorageTransformPlugin.js +3 -1
  218. package/plugins/index.js +3 -1
  219. package/storage/default.js +3 -1
  220. package/storage/object.js +3 -1
  221. package/types.d.ts +62 -230
  222. package/types.js +3 -17
  223. package/types.js.map +1 -1
  224. package/utils/access.js +3 -1
  225. package/utils/caching/Cache.d.ts +2 -0
  226. package/utils/caching/Cache.js +53 -0
  227. package/utils/caching/Cache.js.map +1 -0
  228. package/utils/caching/CacheKey.d.ts +3 -0
  229. package/utils/caching/CacheKey.js +41 -0
  230. package/utils/caching/CacheKey.js.map +1 -0
  231. package/utils/caching/index.d.ts +2 -0
  232. package/utils/caching/index.js +29 -0
  233. package/utils/caching/index.js.map +1 -0
  234. package/utils/caching/types.d.ts +9 -0
  235. package/utils/caching/types.js +7 -0
  236. package/utils/caching/types.js.map +1 -0
  237. package/utils/converters/Converter.js +3 -1
  238. package/utils/converters/ConverterCollection.js +3 -1
  239. package/utils/converters/valueKeyStorageConverter.js +3 -1
  240. package/utils/createTypeFromFields.js +6 -1
  241. package/utils/createTypeFromFields.js.map +1 -1
  242. package/utils/createTypeName.js +3 -1
  243. package/utils/entryStorage.js +3 -1
  244. package/utils/filterAsync.js +3 -1
  245. package/utils/getBaseFieldType.js +3 -1
  246. package/utils/getEntryDescription.js +3 -1
  247. package/utils/getEntryImage.js +3 -1
  248. package/utils/getEntryTitle.js +3 -1
  249. package/utils/getSchemaFromFieldPlugins.js +3 -1
  250. package/utils/incrementEntryIdVersion.js +3 -1
  251. package/utils/index.d.ts +1 -0
  252. package/utils/index.js +18 -0
  253. package/utils/index.js.map +1 -0
  254. package/utils/permissions/EntriesPermissions.js +3 -1
  255. package/utils/permissions/ModelGroupsPermissions.js +3 -1
  256. package/utils/permissions/ModelsPermissions.js +3 -1
  257. package/utils/renderFields.js +3 -1
  258. package/utils/renderGetFilterFields.js +3 -1
  259. package/utils/renderInputFields.js +3 -1
  260. package/utils/renderListFilterFields.js +3 -1
  261. package/utils/renderSortEnum.js +3 -1
  262. package/utils/toSlug.js +3 -1
  263. package/validators/dateGte.js +3 -1
  264. package/validators/dateLte.js +3 -1
  265. package/validators/gte.js +3 -1
  266. package/validators/in.js +3 -1
  267. package/validators/index.js +4 -3
  268. package/validators/index.js.map +1 -1
  269. package/validators/lte.js +3 -1
  270. package/validators/maxLength.js +3 -1
  271. package/validators/minLength.js +3 -1
  272. package/validators/pattern.js +3 -1
  273. package/validators/patternPlugins/email.js +3 -1
  274. package/validators/patternPlugins/index.js +3 -1
  275. package/validators/patternPlugins/lowerCase.js +3 -1
  276. package/validators/patternPlugins/lowerCaseSpace.js +3 -1
  277. package/validators/patternPlugins/upperCase.js +3 -1
  278. package/validators/patternPlugins/upperCaseSpace.js +3 -1
  279. package/validators/patternPlugins/url.js +3 -1
  280. package/validators/required.js +3 -1
  281. package/validators/timeGte.js +3 -1
  282. package/validators/timeLte.js +3 -1
  283. package/validators/unique.js +4 -2
  284. package/validators/unique.js.map +1 -1
  285. package/crud/contentModel/afterCreate.d.ts +0 -8
  286. package/crud/contentModel/afterCreate.js +0 -16
  287. package/crud/contentModel/afterCreate.js.map +0 -1
  288. package/crud/contentModel/afterCreateFrom.d.ts +0 -8
  289. package/crud/contentModel/afterCreateFrom.js +0 -16
  290. package/crud/contentModel/afterCreateFrom.js.map +0 -1
  291. package/crud/contentModel/afterDelete.d.ts +0 -8
  292. package/crud/contentModel/afterDelete.js +0 -16
  293. package/crud/contentModel/afterDelete.js.map +0 -1
  294. package/crud/contentModel/afterUpdate.d.ts +0 -8
  295. package/crud/contentModel/afterUpdate.js +0 -16
  296. package/crud/contentModel/afterUpdate.js.map +0 -1
  297. package/crud/contentModel/validateLayout.d.ts +0 -2
  298. package/crud/contentModel/validateLayout.js +0 -28
  299. package/crud/contentModel/validateLayout.js.map +0 -1
  300. package/crud/settings.crud.d.ts +0 -12
  301. package/crud/settings.crud.js +0 -62
  302. package/crud/settings.crud.js.map +0 -1
  303. package/utils/permissions/SettingsPermissions.d.ts +0 -4
  304. package/utils/permissions/SettingsPermissions.js +0 -9
  305. package/utils/permissions/SettingsPermissions.js.map +0 -1
  306. package/validators/dynamicZone.d.ts +0 -2
  307. package/validators/dynamicZone.js +0 -20
  308. package/validators/dynamicZone.js.map +0 -1
@@ -28,7 +28,7 @@ const buildReferenceFieldPaths = params => {
28
28
  const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(".")}.` : "";
29
29
  if (field.multipleValues) {
30
30
  const inputValue = _dotProp.default.get(input, `${field.fieldId}`, []);
31
- if (Array.isArray(inputValue) === false) {
31
+ if (!Array.isArray(inputValue)) {
32
32
  return collection;
33
33
  }
34
34
  for (const key in inputValue) {
@@ -50,29 +50,41 @@ const buildReferenceFieldPaths = params => {
50
50
  if (baseType === "dynamicZone") {
51
51
  var _field$settings;
52
52
  const templates = ((_field$settings = field.settings) === null || _field$settings === void 0 ? void 0 : _field$settings.templates) || [];
53
- for (const template of templates) {
54
- if (field.multipleValues) {
55
- const inputValue = _dotProp.default.get(input, `${field.fieldId}`, []);
56
- if (Array.isArray(inputValue) === false) {
57
- return collection;
58
- }
59
- for (const key in inputValue) {
60
- const result = buildReferenceFieldPaths({
61
- fields: template.fields,
62
- input: inputValue[key],
63
- parentPaths: parentPaths.concat([field.fieldId, key, template.gqlTypeName])
64
- });
65
- collection.push(...result);
66
- }
67
- continue;
53
+ if (field.multipleValues) {
54
+ const values = _dotProp.default.get(input, field.fieldId, []);
55
+ if (!Array.isArray(values)) {
56
+ return collection;
68
57
  }
69
- const result = buildReferenceFieldPaths({
70
- fields: template.fields,
71
- input,
72
- parentPaths: parentPaths.concat([field.fieldId, template.gqlTypeName])
58
+ values.forEach((value, index) => {
59
+ const valueTemplate = Object.keys(value)[0];
60
+ const template = templates.find(tpl => tpl.gqlTypeName === valueTemplate);
61
+ if (!template) {
62
+ return;
63
+ }
64
+ const result = buildReferenceFieldPaths({
65
+ fields: template.fields,
66
+ input: value[valueTemplate],
67
+ parentPaths: parentPaths.concat([field.fieldId, String(index), template.gqlTypeName])
68
+ });
69
+ collection.push(...result);
73
70
  });
74
- collection.push(...result);
71
+ return collection;
75
72
  }
73
+ const value = _dotProp.default.get(input, field.fieldId, {});
74
+ if (!value) {
75
+ return collection;
76
+ }
77
+ const valueTemplate = Object.keys(value)[0];
78
+ const template = templates.find(tpl => tpl.gqlTypeName === valueTemplate);
79
+ if (!template) {
80
+ return collection;
81
+ }
82
+ const result = buildReferenceFieldPaths({
83
+ fields: template.fields,
84
+ input: _dotProp.default.get(value, valueTemplate, {}),
85
+ parentPaths: parentPaths.concat([field.fieldId, template.gqlTypeName])
86
+ });
87
+ collection.push(...result);
76
88
  return collection;
77
89
  }
78
90
 
@@ -135,6 +147,13 @@ const getReferenceFieldValue = ref => {
135
147
  modelId: (ref.modelId || "").trim() || null
136
148
  };
137
149
  };
150
+
151
+ /**
152
+ * This function traverses the content entry input value, extracts all occurrences of the `ref` field,
153
+ * optionally verifies that those referenced entries exist (by loading them), and normalizes the `ref` value to
154
+ * always contain `{ id, modelId, entryId }`. `entryId` is important when data is being loaded via
155
+ * the `read` and `preview` endpoint.
156
+ */
138
157
  const referenceFieldsMapping = async params => {
139
158
  const {
140
159
  context,
@@ -148,12 +167,62 @@ const referenceFieldsMapping = async params => {
148
167
  input,
149
168
  parentPaths: []
150
169
  });
151
- if (referenceFieldPaths.length === 0) {
170
+ if (!referenceFieldPaths.length) {
152
171
  return output;
153
172
  }
154
- const referencesByModel = {};
155
- const pathsByReferenceId = {};
173
+ if (validateEntries) {
174
+ await validateReferencedEntries({
175
+ output,
176
+ context,
177
+ referenceFieldPaths
178
+ });
179
+ }
180
+
181
+ /**
182
+ * Assign the entryId, id and model values to the output.
183
+ */
184
+ for (const path of referenceFieldPaths) {
185
+ // It is safe to cast here, because `referenceFieldPaths` array is generated from the `input`.
186
+ const refValue = _dotProp.default.get(input, path);
187
+ if (!refValue) {
188
+ continue;
189
+ }
190
+
191
+ /**
192
+ * Over time, the structure of `RefInput` was changing, and we need to handle different cases for backwards
193
+ * compatibility. The latest valid structure of a `ref` field value is { id, modelId }, but we also need
194
+ * to make sure that the legacy structure { entryId, modelId } is supported.
195
+ */
196
+ const {
197
+ id,
198
+ modelId,
199
+ entryId: maybeEntryId
200
+ } = refValue;
201
+ const {
202
+ id: entryId
203
+ } = (0, _utils.parseIdentifier)(maybeEntryId || id);
204
+ output = _dotProp.default.set(output, path, {
205
+ // If `id` is not set, we're dealing with the legacy structure.
206
+ id: id ?? maybeEntryId,
207
+ entryId,
208
+ modelId
209
+ });
210
+ }
211
+ return output;
212
+ };
213
+ exports.referenceFieldsMapping = referenceFieldsMapping;
214
+ async function validateReferencedEntries({
215
+ output,
216
+ context,
217
+ referenceFieldPaths
218
+ }) {
219
+ const referencesByModel = new Map();
220
+
221
+ /**
222
+ * Group references by modelId.
223
+ */
156
224
  for (const path of referenceFieldPaths) {
225
+ var _referencesByModel$ge;
157
226
  const ref = _dotProp.default.get(output, path);
158
227
  const {
159
228
  id,
@@ -162,108 +231,60 @@ const referenceFieldsMapping = async params => {
162
231
  if (!id || !modelId) {
163
232
  continue;
164
233
  }
165
- if (!referencesByModel[modelId]) {
166
- referencesByModel[modelId] = [];
167
- }
168
- referencesByModel[modelId].push(id);
169
- if (!pathsByReferenceId[id]) {
170
- pathsByReferenceId[id] = [];
234
+ if (!referencesByModel.has(modelId)) {
235
+ referencesByModel.set(modelId, []);
171
236
  }
172
- pathsByReferenceId[id].push(path);
237
+ (_referencesByModel$ge = referencesByModel.get(modelId)) === null || _referencesByModel$ge === void 0 ? void 0 : _referencesByModel$ge.push(id);
173
238
  }
174
-
175
- /**
176
- * Again, no point in going further.
177
- */
178
- if (Object.keys(referencesByModel).length === 0) {
179
- return output;
239
+ if (!referencesByModel.size) {
240
+ return;
180
241
  }
242
+
181
243
  /**
182
244
  * Load all models and use only those that are used in reference.
183
245
  */
184
246
  const models = await context.security.withoutAuthorization(async () => {
185
247
  return (await context.cms.listModels()).filter(model => {
186
- const entries = referencesByModel[model.modelId];
248
+ const entries = referencesByModel.get(model.modelId);
187
249
  if (!Array.isArray(entries) || entries.length === 0) {
188
250
  return false;
189
251
  }
190
252
  return true;
191
253
  });
192
254
  });
193
- /**
194
- * Check for any model existence, just in case.
195
- */
196
- if (models.length === 0) {
197
- return output;
255
+ if (!models.length) {
256
+ return;
198
257
  }
199
258
 
200
259
  /**
201
- * Load all the entries by their ID
260
+ * Load all the entries by their IDs.
202
261
  */
203
262
  const promises = await context.security.withoutAuthorization(async () => {
204
263
  return models.map(model => {
205
- return context.cms.getEntriesByIds(model, referencesByModel[model.modelId]);
264
+ return context.cms.getEntriesByIds(model, referencesByModel.get(model.modelId) || []);
206
265
  });
207
266
  });
208
- const results = await Promise.all(promises);
209
- const records = results.reduce((collection, entries) => {
210
- for (const entry of entries) {
211
- collection[entry.id] = {
212
- id: entry.id,
213
- entryId: entry.entryId,
214
- modelId: entry.modelId
215
- };
216
- }
217
- return collection;
267
+ const allEntries = await Promise.all(promises).then(res => res.flat());
268
+ const entriesByModel = allEntries.reduce((acc, entry) => {
269
+ return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, acc), {}, {
270
+ [entry.modelId]: [...(acc[entry.modelId] || []), entry.id]
271
+ });
218
272
  }, {});
273
+
219
274
  /**
220
- * Verify that all referenced entries actually exist.
275
+ * Verify that all entries exist.
221
276
  */
222
- for (const modelId in referencesByModel) {
223
- const entries = referencesByModel[modelId];
224
- for (const id of entries) {
225
- if (records[id]) {
226
- continue;
227
- } else if (validateEntries) {
277
+ referencesByModel.forEach((ids, modelId) => {
278
+ const modelEntriesInDb = entriesByModel[modelId];
279
+ for (const id of ids) {
280
+ if (!modelEntriesInDb.includes(id)) {
228
281
  throw new _error.default(`Missing referenced entry with id "${id}" in model "${modelId}".`, "ENTRY_NOT_FOUND", {
229
282
  id,
230
283
  model: modelId
231
284
  });
232
285
  }
233
- const {
234
- id: entryId
235
- } = (0, _utils.parseIdentifier)(id);
236
- records[id] = {
237
- id,
238
- entryId,
239
- modelId
240
- };
241
286
  }
242
- }
287
+ });
288
+ }
243
289
 
244
- /**
245
- * In the end, assign the entryId, id and model values to the output.
246
- */
247
- for (const id in pathsByReferenceId) {
248
- const entry = records[id];
249
- const paths = pathsByReferenceId[id];
250
- if (!entry) {
251
- if (validateEntries) {
252
- throw new _error.default("Missing entry in records.", "ENTRY_ERROR", {
253
- id,
254
- paths
255
- });
256
- }
257
- continue;
258
- }
259
- for (const path of paths) {
260
- output = _dotProp.default.set(output, path, {
261
- id: entry.id,
262
- entryId: entry.entryId,
263
- modelId: entry.modelId
264
- });
265
- }
266
- }
267
- return output;
268
- };
269
- exports.referenceFieldsMapping = referenceFieldsMapping;
290
+ //# sourceMappingURL=referenceFieldsMapping.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_error","_interopRequireDefault","require","_dotProp","_utils","_getBaseFieldType","buildReferenceFieldPaths","params","fields","parentPaths","initialParentPaths","input","isMultipleValues","Array","isArray","filter","field","includes","getBaseFieldType","reduce","collection","_field$settings3","baseType","parentPathsValue","length","join","multipleValues","inputValue","dotProp","get","fieldId","key","path","push","_field$settings","templates","settings","template","result","concat","gqlTypeName","objFieldPath","objFieldInputValue","_field$settings2","results","getReferenceFieldValue","ref","id","modelId","entryId","trim","referenceFieldsMapping","context","model","validateEntries","output","_objectSpread2","default","referenceFieldPaths","referencesByModel","pathsByReferenceId","Object","keys","models","security","withoutAuthorization","cms","listModels","entries","promises","map","getEntriesByIds","Promise","all","records","entry","WebinyError","parseIdentifier","paths","set","exports"],"sources":["referenceFieldsMapping.ts"],"sourcesContent":["import { CmsContext, CmsDynamicZoneTemplate, CmsModel, CmsModelField } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport dotProp from \"dot-prop\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { getBaseFieldType } from \"~/utils/getBaseFieldType\";\n\ninterface CmsRefEntry {\n id: string;\n entryId: string;\n modelId: string;\n}\n\ninterface ReferenceObject {\n id: string;\n modelId: string;\n}\n\ninterface Params {\n context: CmsContext;\n model: CmsModel;\n input: Record<string, ReferenceObject | ReferenceObject[]>;\n validateEntries?: boolean;\n}\n\ninterface BuildReferenceFieldPaths {\n fields: CmsModelField[];\n parentPaths: string[];\n input: Record<string, any>;\n}\n\nconst buildReferenceFieldPaths = (params: BuildReferenceFieldPaths): string[] => {\n const { fields, parentPaths: initialParentPaths, input } = params;\n\n const parentPaths = [...initialParentPaths];\n\n const isMultipleValues = Array.isArray(input);\n\n return fields\n .filter(field => [\"object\", \"ref\", \"dynamicZone\"].includes(getBaseFieldType(field)))\n .reduce((collection, field) => {\n /**\n * First we check the ref field\n */\n const baseType = getBaseFieldType(field);\n if (baseType === \"ref\") {\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n if (field.multipleValues) {\n const inputValue = dotProp.get(input, `${field.fieldId}`, []);\n if (Array.isArray(inputValue) === false) {\n return collection;\n }\n for (const key in inputValue) {\n const path = `${parentPathsValue}${field.fieldId}.${key}`;\n collection.push(path);\n }\n return collection;\n }\n\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n collection.push(`${parentPathsValue}${field.fieldId}`);\n\n return collection;\n }\n\n if (baseType === \"dynamicZone\") {\n const templates: CmsDynamicZoneTemplate[] = field.settings?.templates || [];\n for (const template of templates) {\n if (field.multipleValues) {\n const inputValue = dotProp.get(input, `${field.fieldId}`, []);\n if (Array.isArray(inputValue) === false) {\n return collection;\n }\n\n for (const key in inputValue) {\n const result = buildReferenceFieldPaths({\n fields: template.fields,\n input: inputValue[key],\n parentPaths: parentPaths.concat([\n field.fieldId,\n key,\n template.gqlTypeName\n ])\n });\n collection.push(...result);\n }\n continue;\n }\n\n const result = buildReferenceFieldPaths({\n fields: template.fields,\n input,\n parentPaths: parentPaths.concat([field.fieldId, template.gqlTypeName])\n });\n collection.push(...result);\n }\n\n return collection;\n }\n\n /**\n * Then we move onto the object field\n */\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n /**\n * This is if received input is array. We need to map key with fieldId at this point.\n */\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n const objFieldPath = `${field.fieldId}`;\n const objFieldInputValue = dotProp.get(input, objFieldPath, []);\n\n /**\n * If field is multiple values one, we need to go through the input and use the existing keys.\n */\n if (field.multipleValues) {\n if (Array.isArray(objFieldInputValue) === false) {\n return collection;\n }\n for (const key in objFieldInputValue) {\n const result = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue[key],\n parentPaths: parentPaths.concat([field.fieldId, key])\n });\n collection.push(...result);\n }\n\n return collection;\n }\n\n /**\n * Single value reference field.\n */\n const results = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue,\n parentPaths: parentPaths.concat([field.fieldId])\n });\n\n return collection.concat(results);\n }, [] as string[]);\n};\n\nconst getReferenceFieldValue = (ref: any): { id: string | null; modelId: string | null } => {\n if (!ref) {\n return {\n id: null,\n modelId: null\n };\n }\n return {\n id: (ref.id || ref.entryId || \"\").trim() || null,\n modelId: (ref.modelId || \"\").trim() || null\n };\n};\n\nexport const referenceFieldsMapping = async (params: Params): Promise<Record<string, any>> => {\n const { context, model, input, validateEntries = false } = params;\n\n let output: Record<string, any> = {\n ...input\n };\n\n const referenceFieldPaths = buildReferenceFieldPaths({\n fields: model.fields,\n input,\n parentPaths: []\n });\n if (referenceFieldPaths.length === 0) {\n return output;\n }\n\n const referencesByModel: Record<string, string[]> = {};\n const pathsByReferenceId: Record<string, string[]> = {};\n\n for (const path of referenceFieldPaths) {\n const ref = dotProp.get(output, path) as ReferenceObject | any;\n\n const { id, modelId } = getReferenceFieldValue(ref);\n\n if (!id || !modelId) {\n continue;\n }\n if (!referencesByModel[modelId]) {\n referencesByModel[modelId] = [];\n }\n referencesByModel[modelId].push(id);\n if (!pathsByReferenceId[id]) {\n pathsByReferenceId[id] = [];\n }\n pathsByReferenceId[id].push(path);\n }\n\n /**\n * Again, no point in going further.\n */\n if (Object.keys(referencesByModel).length === 0) {\n return output;\n }\n /**\n * Load all models and use only those that are used in reference.\n */\n const models = await context.security.withoutAuthorization(async () => {\n return (await context.cms.listModels()).filter(model => {\n const entries = referencesByModel[model.modelId];\n if (!Array.isArray(entries) || entries.length === 0) {\n return false;\n }\n return true;\n });\n });\n /**\n * Check for any model existence, just in case.\n */\n if (models.length === 0) {\n return output;\n }\n\n /**\n * Load all the entries by their ID\n */\n const promises = await context.security.withoutAuthorization(async () => {\n return models.map(model => {\n return context.cms.getEntriesByIds(model, referencesByModel[model.modelId]);\n });\n });\n\n const results = await Promise.all(promises);\n\n const records: Record<string, CmsRefEntry> = results.reduce((collection, entries) => {\n for (const entry of entries) {\n collection[entry.id] = {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n };\n }\n return collection;\n }, {} as Record<string, CmsRefEntry>);\n /**\n * Verify that all referenced entries actually exist.\n */\n for (const modelId in referencesByModel) {\n const entries = referencesByModel[modelId];\n for (const id of entries) {\n if (records[id]) {\n continue;\n } else if (validateEntries) {\n throw new WebinyError(\n `Missing referenced entry with id \"${id}\" in model \"${modelId}\".`,\n \"ENTRY_NOT_FOUND\",\n {\n id,\n model: modelId\n }\n );\n }\n const { id: entryId } = parseIdentifier(id);\n records[id] = {\n id,\n entryId,\n modelId\n };\n }\n }\n\n /**\n * In the end, assign the entryId, id and model values to the output.\n */\n for (const id in pathsByReferenceId) {\n const entry = records[id];\n const paths = pathsByReferenceId[id];\n if (!entry) {\n if (validateEntries) {\n throw new WebinyError(\"Missing entry in records.\", \"ENTRY_ERROR\", {\n id,\n paths\n });\n }\n continue;\n }\n for (const path of paths) {\n output = dotProp.set(output, path, {\n id: entry.id,\n entryId: entry.entryId,\n modelId: entry.modelId\n });\n }\n }\n\n return output;\n};\n"],"mappings":";;;;;;;;AACA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,QAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,iBAAA,GAAAH,OAAA;AA0BA,MAAMI,wBAAwB,GAAIC,MAAgC,IAAe;E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MAAM,IAAIsB,cAAW,CAChB,qCAAoC7B,EAAG,eAAcC,OAAQ,IAAG,EACjE,iBAAiB,EACjB;UACID,EAAE;UACFM,KAAK,EAAEL;QACX,CACJ,CAAC;MACL;MACA,MAAM;QAAED,EAAE,EAAEE;MAAQ,CAAC,GAAG,IAAA4B,sBAAe,EAAC9B,EAAE,CAAC;MAC3C2B,OAAO,CAAC3B,EAAE,CAAC,GAAG;QACVA,EAAE;QACFE,OAAO;QACPD;MACJ,CAAC;IACL;EACJ;;EAEA;AACJ;AACA;EACI,KAAK,MAAMD,EAAE,IAAIa,kBAAkB,EAAE;IACjC,MAAMe,KAAK,GAAGD,OAAO,CAAC3B,EAAE,CAAC;IACzB,MAAM+B,KAAK,GAAGlB,kBAAkB,CAACb,EAAE,CAAC;IACpC,IAAI,CAAC4B,KAAK,EAAE;MACR,IAAIrB,eAAe,EAAE;QACjB,MAAM,IAAIsB,cAAW,CAAC,2BAA2B,EAAE,aAAa,EAAE;UAC9D7B,EAAE;UACF+B;QACJ,CAAC,CAAC;MACN;MACA;IACJ;IACA,KAAK,MAAM9C,IAAI,IAAI8C,KAAK,EAAE;MACtBvB,MAAM,GAAG3B,gBAAO,CAACmD,GAAG,CAACxB,MAAM,EAAEvB,IAAI,EAAE;QAC/Be,EAAE,EAAE4B,KAAK,CAAC5B,EAAE;QACZE,OAAO,EAAE0B,KAAK,CAAC1B,OAAO;QACtBD,OAAO,EAAE2B,KAAK,CAAC3B;MACnB,CAAC,CAAC;IACN;EACJ;EAEA,OAAOO,MAAM;AACjB,CAAC;AAACyB,OAAA,CAAA7B,sBAAA,GAAAA,sBAAA"}
1
+ {"version":3,"names":["_error","_interopRequireDefault","require","_dotProp","_utils","_getBaseFieldType","buildReferenceFieldPaths","params","fields","parentPaths","initialParentPaths","input","isMultipleValues","Array","isArray","filter","field","includes","getBaseFieldType","reduce","collection","_field$settings3","baseType","parentPathsValue","length","join","multipleValues","inputValue","dotProp","get","fieldId","key","path","push","_field$settings","templates","settings","values","forEach","value","index","valueTemplate","Object","keys","template","find","tpl","gqlTypeName","result","concat","String","objFieldPath","objFieldInputValue","_field$settings2","results","getReferenceFieldValue","ref","id","modelId","entryId","trim","referenceFieldsMapping","context","model","validateEntries","output","_objectSpread2","default","referenceFieldPaths","validateReferencedEntries","refValue","maybeEntryId","parseIdentifier","set","exports","referencesByModel","Map","_referencesByModel$ge","has","size","models","security","withoutAuthorization","cms","listModels","entries","promises","map","getEntriesByIds","allEntries","Promise","all","then","res","flat","entriesByModel","acc","entry","ids","modelEntriesInDb","WebinyError"],"sources":["referenceFieldsMapping.ts"],"sourcesContent":["import { CmsContext, CmsDynamicZoneTemplate, CmsModel, CmsModelField } from \"~/types\";\nimport WebinyError from \"@webiny/error\";\nimport dotProp from \"dot-prop\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { getBaseFieldType } from \"~/utils/getBaseFieldType\";\n\ninterface CmsRefEntry {\n id: string;\n entryId: string;\n modelId: string;\n}\n\ntype RefValue = Pick<CmsRefEntry, \"id\" | \"modelId\"> & {\n entryId?: string;\n};\n\ninterface ReferenceObject {\n id: string;\n modelId: string;\n}\n\ninterface Params {\n context: CmsContext;\n model: CmsModel;\n input: Record<string, ReferenceObject | ReferenceObject[]>;\n validateEntries?: boolean;\n}\n\ninterface BuildReferenceFieldPaths {\n fields: CmsModelField[];\n parentPaths: string[];\n input: Record<string, any>;\n}\n\nconst buildReferenceFieldPaths = (params: BuildReferenceFieldPaths): string[] => {\n const { fields, parentPaths: initialParentPaths, input } = params;\n\n const parentPaths = [...initialParentPaths];\n\n const isMultipleValues = Array.isArray(input);\n\n return fields\n .filter(field => [\"object\", \"ref\", \"dynamicZone\"].includes(getBaseFieldType(field)))\n .reduce((collection, field) => {\n /**\n * First we check the ref field\n */\n const baseType = getBaseFieldType(field);\n if (baseType === \"ref\") {\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n if (field.multipleValues) {\n const inputValue = dotProp.get(input, `${field.fieldId}`, []);\n if (!Array.isArray(inputValue)) {\n return collection;\n }\n\n for (const key in inputValue) {\n const path = `${parentPathsValue}${field.fieldId}.${key}`;\n collection.push(path);\n }\n return collection;\n }\n\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n collection.push(`${parentPathsValue}${field.fieldId}`);\n\n return collection;\n }\n\n if (baseType === \"dynamicZone\") {\n const templates: CmsDynamicZoneTemplate[] = field.settings?.templates || [];\n\n if (field.multipleValues) {\n const values = dotProp.get(input, field.fieldId, []);\n if (!Array.isArray(values)) {\n return collection;\n }\n\n values.forEach((value, index) => {\n const valueTemplate = Object.keys(value)[0];\n const template = templates.find(tpl => tpl.gqlTypeName === valueTemplate);\n if (!template) {\n return;\n }\n\n const result = buildReferenceFieldPaths({\n fields: template.fields,\n input: value[valueTemplate],\n parentPaths: parentPaths.concat([\n field.fieldId,\n String(index),\n template.gqlTypeName\n ])\n });\n\n collection.push(...result);\n });\n\n return collection;\n }\n\n const value = dotProp.get(input, field.fieldId, {});\n if (!value) {\n return collection;\n }\n\n const valueTemplate = Object.keys(value)[0];\n const template = templates.find(tpl => tpl.gqlTypeName === valueTemplate);\n\n if (!template) {\n return collection;\n }\n\n const result = buildReferenceFieldPaths({\n fields: template.fields,\n input: dotProp.get(value, valueTemplate, {}),\n parentPaths: parentPaths.concat([field.fieldId, template.gqlTypeName])\n });\n collection.push(...result);\n\n return collection;\n }\n\n /**\n * Then we move onto the object field\n */\n const parentPathsValue = parentPaths.length > 0 ? `${parentPaths.join(\".\")}.` : \"\";\n /**\n * This is if received input is array. We need to map key with fieldId at this point.\n */\n if (isMultipleValues) {\n for (const key in input) {\n const path = `${parentPathsValue}${key}.${field.fieldId}`;\n collection.push(path);\n }\n return collection;\n }\n\n const objFieldPath = `${field.fieldId}`;\n const objFieldInputValue = dotProp.get(input, objFieldPath, []);\n\n /**\n * If field is multiple values one, we need to go through the input and use the existing keys.\n */\n if (field.multipleValues) {\n if (Array.isArray(objFieldInputValue) === false) {\n return collection;\n }\n for (const key in objFieldInputValue) {\n const result = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue[key],\n parentPaths: parentPaths.concat([field.fieldId, key])\n });\n collection.push(...result);\n }\n\n return collection;\n }\n\n /**\n * Single value reference field.\n */\n const results = buildReferenceFieldPaths({\n fields: field.settings?.fields || [],\n input: objFieldInputValue,\n parentPaths: parentPaths.concat([field.fieldId])\n });\n\n return collection.concat(results);\n }, [] as string[]);\n};\n\nconst getReferenceFieldValue = (ref: any): { id: string | null; modelId: string | null } => {\n if (!ref) {\n return {\n id: null,\n modelId: null\n };\n }\n return {\n id: (ref.id || ref.entryId || \"\").trim() || null,\n modelId: (ref.modelId || \"\").trim() || null\n };\n};\n\n/**\n * This function traverses the content entry input value, extracts all occurrences of the `ref` field,\n * optionally verifies that those referenced entries exist (by loading them), and normalizes the `ref` value to\n * always contain `{ id, modelId, entryId }`. `entryId` is important when data is being loaded via\n * the `read` and `preview` endpoint.\n */\nexport const referenceFieldsMapping = async (params: Params): Promise<Record<string, any>> => {\n const { context, model, input, validateEntries = false } = params;\n\n let output: Record<string, any> = {\n ...input\n };\n\n const referenceFieldPaths = buildReferenceFieldPaths({\n fields: model.fields,\n input,\n parentPaths: []\n });\n\n if (!referenceFieldPaths.length) {\n return output;\n }\n\n if (validateEntries) {\n await validateReferencedEntries({ output, context, referenceFieldPaths });\n }\n\n /**\n * Assign the entryId, id and model values to the output.\n */\n for (const path of referenceFieldPaths) {\n // It is safe to cast here, because `referenceFieldPaths` array is generated from the `input`.\n const refValue: RefValue | undefined = dotProp.get(input, path);\n if (!refValue) {\n continue;\n }\n\n /**\n * Over time, the structure of `RefInput` was changing, and we need to handle different cases for backwards\n * compatibility. The latest valid structure of a `ref` field value is { id, modelId }, but we also need\n * to make sure that the legacy structure { entryId, modelId } is supported.\n */\n const { id, modelId, entryId: maybeEntryId } = refValue;\n\n const { id: entryId } = parseIdentifier(maybeEntryId || id);\n\n output = dotProp.set(output, path, {\n // If `id` is not set, we're dealing with the legacy structure.\n id: id ?? maybeEntryId,\n entryId,\n modelId\n });\n }\n\n return output;\n};\n\ninterface ValidateReferencedEntriesParams {\n output: Record<string, any>;\n context: CmsContext;\n referenceFieldPaths: string[];\n}\n\nasync function validateReferencedEntries({\n output,\n context,\n referenceFieldPaths\n}: ValidateReferencedEntriesParams) {\n const referencesByModel = new Map<string, string[]>();\n\n /**\n * Group references by modelId.\n */\n for (const path of referenceFieldPaths) {\n const ref = dotProp.get(output, path) as ReferenceObject | any;\n\n const { id, modelId } = getReferenceFieldValue(ref);\n\n if (!id || !modelId) {\n continue;\n }\n\n if (!referencesByModel.has(modelId)) {\n referencesByModel.set(modelId, []);\n }\n\n referencesByModel.get(modelId)?.push(id);\n }\n\n if (!referencesByModel.size) {\n return;\n }\n\n /**\n * Load all models and use only those that are used in reference.\n */\n const models = await context.security.withoutAuthorization(async () => {\n return (await context.cms.listModels()).filter(model => {\n const entries = referencesByModel.get(model.modelId);\n if (!Array.isArray(entries) || entries.length === 0) {\n return false;\n }\n return true;\n });\n });\n\n if (!models.length) {\n return;\n }\n\n /**\n * Load all the entries by their IDs.\n */\n const promises = await context.security.withoutAuthorization(async () => {\n return models.map(model => {\n return context.cms.getEntriesByIds(model, referencesByModel.get(model.modelId) || []);\n });\n });\n\n const allEntries = await Promise.all(promises).then(res => res.flat());\n const entriesByModel = allEntries.reduce<Record<string, string[]>>((acc, entry) => {\n return { ...acc, [entry.modelId]: [...(acc[entry.modelId] || []), entry.id] };\n }, {});\n\n /**\n * Verify that all entries exist.\n */\n referencesByModel.forEach((ids, modelId) => {\n const modelEntriesInDb = entriesByModel[modelId];\n for (const id of ids) {\n if (!modelEntriesInDb.includes(id)) {\n throw new WebinyError(\n `Missing referenced entry with id \"${id}\" in model \"${modelId}\".`,\n \"ENTRY_NOT_FOUND\",\n {\n id,\n model: modelId\n }\n );\n }\n }\n });\n}\n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AA,MAAAD,cAAA,CAAAC,OAAA,MAAY6B,GAAG;MAAE,CAACC,KAAK,CAACvC,OAAO,GAAG,CAAC,IAAIsC,GAAG,CAACC,KAAK,CAACvC,OAAO,CAAC,IAAI,EAAE,CAAC,EAAEuC,KAAK,CAACxC,EAAE;IAAC;EAC/E,CAAC,EAAE,CAAC,CAAC,CAAC;;EAEN;AACJ;AACA;EACIkB,iBAAiB,CAACrC,OAAO,CAAC,CAAC4D,GAAG,EAAExC,OAAO,KAAK;IACxC,MAAMyC,gBAAgB,GAAGJ,cAAc,CAACrC,OAAO,CAAC;IAChD,KAAK,MAAMD,EAAE,IAAIyC,GAAG,EAAE;MAClB,IAAI,CAACC,gBAAgB,CAAClF,QAAQ,CAACwC,EAAE,CAAC,EAAE;QAChC,MAAM,IAAI2C,cAAW,CAChB,qCAAoC3C,EAAG,eAAcC,OAAQ,IAAG,EACjE,iBAAiB,EACjB;UACID,EAAE;UACFM,KAAK,EAAEL;QACX,CACJ,CAAC;MACL;IACJ;EACJ,CAAC,CAAC;AACN"}
@@ -12,7 +12,7 @@ const buildSearchableFieldList = params => {
12
12
  parents
13
13
  } = params;
14
14
  return fields.reduce((result, field) => {
15
- var _field$settings;
15
+ var _field$settings, _field$settings2;
16
16
  /**
17
17
  * We need to check if the field is full text searchable, and for that we need a plugin for the field type.
18
18
  */
@@ -40,7 +40,7 @@ const buildSearchableFieldList = params => {
40
40
  /**
41
41
  * If not searchable, continue further.
42
42
  */
43
- if (!plugin.fullTextSearch) {
43
+ if (!plugin.fullTextSearch || ((_field$settings2 = field.settings) === null || _field$settings2 === void 0 ? void 0 : _field$settings2.disableFullTextSearch) === true) {
44
44
  return result;
45
45
  }
46
46
 
@@ -69,4 +69,6 @@ const getSearchableFields = params => {
69
69
  parents: []
70
70
  });
71
71
  };
72
- exports.getSearchableFields = getSearchableFields;
72
+ exports.getSearchableFields = getSearchableFields;
73
+
74
+ //# sourceMappingURL=searchableFields.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["buildSearchableFieldList","params","input","plugins","fields","parents","reduce","result","field","_field$settings","plugin","type","childFields","settings","length","childResults","fieldId","push","fullTextSearch","path","join","getSearchableFields","fieldPluginMap","byType","collection","fieldType","exports"],"sources":["searchableFields.ts"],"sourcesContent":["import { CmsModelField, CmsModelFieldToGraphQLPlugin } from \"~/types\";\nimport { PluginsContainer } from \"@webiny/plugins\";\n\ninterface BuildParams {\n input: string[];\n fields: CmsModelField[];\n plugins: Record<string, CmsModelFieldToGraphQLPlugin>;\n parents: string[];\n}\nconst buildSearchableFieldList = (params: BuildParams): string[] => {\n const { input, plugins, fields, parents } = params;\n return fields.reduce<string[]>((result, field) => {\n /**\n * We need to check if the field is full text searchable, and for that we need a plugin for the field type.\n */\n const plugin = plugins[field.type];\n if (!plugin) {\n return result;\n }\n /**\n * There is a possibility that searchable fields exist in nested object field, so check that as well.\n */\n const childFields = field.settings?.fields || [];\n if (childFields.length > 0) {\n /**\n * So we build a list of searchable child fields and push it into the main result set.\n */\n const childResults = buildSearchableFieldList({\n fields: childFields,\n parents: [...parents, field.fieldId],\n plugins,\n input\n });\n\n result.push(...childResults);\n return result;\n }\n /**\n * If not searchable, continue further.\n */\n if (!plugin.fullTextSearch) {\n return result;\n }\n\n /**\n * Combine all parent paths with the current one and push it.\n */\n const path = [...parents, field.fieldId].join(\".\");\n result.push(path);\n\n return result;\n }, []);\n};\n\ninterface Params {\n input: string[];\n fields: CmsModelField[];\n plugins: PluginsContainer;\n}\nexport const getSearchableFields = (params: Params): string[] => {\n const { plugins, input, fields } = params;\n const fieldPluginMap = plugins\n .byType<CmsModelFieldToGraphQLPlugin>(\"cms-model-field-to-graphql\")\n .reduce((collection, field) => {\n collection[field.fieldType] = field;\n return collection;\n }, {} as Record<string, CmsModelFieldToGraphQLPlugin>);\n\n return buildSearchableFieldList({\n fields,\n input,\n plugins: fieldPluginMap,\n parents: []\n });\n};\n"],"mappings":";;;;;;AASA,MAAMA,wBAAwB,GAAIC,MAAmB,IAAe;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,MAAM,CAA+B,4BAA4B,CAAC,CAClEjB,MAAM,CAAC,CAACkB,UAAU,EAAEhB,KAAK,KAAK;IAC3BgB,UAAU,CAAChB,KAAK,CAACiB,SAAS,CAAC,GAAGjB,KAAK;IACnC,OAAOgB,UAAU;EACrB,CAAC,EAAE,CAAC,CAAiD,CAAC;EAE1D,OAAOxB,wBAAwB,CAAC;IAC5BI,MAAM;IACNF,KAAK;IACLC,OAAO,EAAEmB,cAAc;IACvBjB,OAAO,EAAE;EACb,CAAC,CAAC;AACN,CAAC;AAACqB,OAAA,CAAAL,mBAAA,GAAAA,mBAAA"}
1
+ {"version":3,"names":["buildSearchableFieldList","params","input","plugins","fields","parents","reduce","result","field","_field$settings","_field$settings2","plugin","type","childFields","settings","length","childResults","fieldId","push","fullTextSearch","disableFullTextSearch","path","join","getSearchableFields","fieldPluginMap","byType","collection","fieldType","exports"],"sources":["searchableFields.ts"],"sourcesContent":["import { CmsModelField, CmsModelFieldToGraphQLPlugin } from \"~/types\";\nimport { PluginsContainer } from \"@webiny/plugins\";\n\ninterface BuildParams {\n input: string[];\n fields: CmsModelField[];\n plugins: Record<string, CmsModelFieldToGraphQLPlugin>;\n parents: string[];\n}\nconst buildSearchableFieldList = (params: BuildParams): string[] => {\n const { input, plugins, fields, parents } = params;\n return fields.reduce<string[]>((result, field) => {\n /**\n * We need to check if the field is full text searchable, and for that we need a plugin for the field type.\n */\n const plugin = plugins[field.type];\n if (!plugin) {\n return result;\n }\n /**\n * There is a possibility that searchable fields exist in nested object field, so check that as well.\n */\n const childFields = field.settings?.fields || [];\n if (childFields.length > 0) {\n /**\n * So we build a list of searchable child fields and push it into the main result set.\n */\n const childResults = buildSearchableFieldList({\n fields: childFields,\n parents: [...parents, field.fieldId],\n plugins,\n input\n });\n\n result.push(...childResults);\n return result;\n }\n /**\n * If not searchable, continue further.\n */\n if (!plugin.fullTextSearch || field.settings?.disableFullTextSearch === true) {\n return result;\n }\n\n /**\n * Combine all parent paths with the current one and push it.\n */\n const path = [...parents, field.fieldId].join(\".\");\n result.push(path);\n\n return result;\n }, []);\n};\n\ninterface Params {\n input: string[];\n fields: CmsModelField[];\n plugins: PluginsContainer;\n}\nexport const getSearchableFields = (params: Params): string[] => {\n const { plugins, input, fields } = params;\n const fieldPluginMap = plugins\n .byType<CmsModelFieldToGraphQLPlugin>(\"cms-model-field-to-graphql\")\n .reduce((collection, field) => {\n collection[field.fieldType] = field;\n return collection;\n }, {} as Record<string, CmsModelFieldToGraphQLPlugin>);\n\n return buildSearchableFieldList({\n fields,\n input,\n plugins: fieldPluginMap,\n parents: []\n });\n};\n"],"mappings":";;;;;;AASA,MAAMA,wBAAwB,GAAIC,MAAmB,IAAe;EAChE,MAAM;IAAEC,KAAK;IAAEC,OAAO;IAAEC,MAAM;IAAEC;EAAQ,CAAC,GAAGJ,MAAM;EAClD,OAAOG,MAAM,CAACE,MAAM,CAAW,CAACC,MAAM,EAAEC,KAAK,KAAK;IAAA,IAAAC,eAAA,EAAAC,gBAAA;IAC9C;AACR;AACA;IACQ,MAAMC,MAAM,GAAGR,OAAO,CAACK,KAAK,CAACI,IAAI,CAAC;IAClC,IAAI,CAACD,MAAM,EAAE;MACT,OAAOJ,MAAM;IACjB;IACA;AACR;AACA;IACQ,MAAMM,WAAW,GAAG,EAAAJ,eAAA,GAAAD,KAAK,CAACM,QAAQ,cAAAL,eAAA,uBAAdA,eAAA,CAAgBL,MAAM,KAAI,EAAE;IAChD,IAAIS,WAAW,CAACE,MAAM,GAAG,CAAC,EAAE;MACxB;AACZ;AACA;MACY,MAAMC,YAAY,GAAGhB,wBAAwB,CAAC;QAC1CI,MAAM,EAAES,WAAW;QACnBR,OAAO,EAAE,CAAC,GAAGA,OAAO,EAAEG,KAAK,CAACS,OAAO,CAAC;QACpCd,OAAO;QACPD;MACJ,CAAC,CAAC;MAEFK,MAAM,CAACW,IAAI,CAAC,GAAGF,YAAY,CAAC;MAC5B,OAAOT,MAAM;IACjB;IACA;AACR;AACA;IACQ,IAA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