@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.
- package/constants.js +3 -1
- package/context.js +10 -17
- package/context.js.map +1 -1
- package/crud/contentEntry/afterDelete.js +3 -1
- package/crud/contentEntry/beforeCreate.js +3 -1
- package/crud/contentEntry/beforeUpdate.js +3 -1
- package/crud/contentEntry/entryDataValidation.d.ts +4 -2
- package/crud/contentEntry/entryDataValidation.js +220 -47
- package/crud/contentEntry/entryDataValidation.js.map +1 -1
- package/crud/contentEntry/markLockedFields.js +3 -1
- package/crud/contentEntry/referenceFieldsMapping.d.ts +6 -0
- package/crud/contentEntry/referenceFieldsMapping.js +118 -97
- package/crud/contentEntry/referenceFieldsMapping.js.map +1 -1
- package/crud/contentEntry/searchableFields.js +5 -3
- package/crud/contentEntry/searchableFields.js.map +1 -1
- package/crud/contentEntry.crud.js +68 -64
- package/crud/contentEntry.crud.js.map +1 -1
- package/crud/contentModel/beforeCreate.d.ts +1 -1
- package/crud/contentModel/beforeCreate.js +11 -9
- package/crud/contentModel/beforeCreate.js.map +1 -1
- package/crud/contentModel/beforeDelete.js +3 -1
- package/crud/contentModel/beforeUpdate.d.ts +1 -1
- package/crud/contentModel/beforeUpdate.js +3 -6
- package/crud/contentModel/beforeUpdate.js.map +1 -1
- package/crud/contentModel/compatibility/modelApiName.js +3 -1
- package/crud/contentModel/contentModelManagerFactory.js +3 -1
- package/crud/contentModel/createFieldStorageId.js +3 -1
- package/crud/contentModel/defaultFields.js +3 -1
- package/crud/contentModel/ensureTypeTag.d.ts +5 -0
- package/crud/contentModel/ensureTypeTag.js +21 -0
- package/crud/contentModel/ensureTypeTag.js.map +1 -0
- package/crud/contentModel/fields/descriptionField.js +3 -1
- package/crud/contentModel/fields/imageField.js +3 -1
- package/crud/contentModel/fields/titleField.js +3 -1
- package/crud/contentModel/listModelsFromDatabase.d.ts +10 -0
- package/crud/contentModel/listModelsFromDatabase.js +39 -0
- package/crud/contentModel/listModelsFromDatabase.js.map +1 -0
- package/crud/contentModel/validate/endingAllowed.js +3 -1
- package/crud/contentModel/validate/isModelEndingAllowed.js +4 -2
- package/crud/contentModel/validate/isModelEndingAllowed.js.map +1 -1
- package/crud/contentModel/validate/modelId.js +3 -1
- package/crud/contentModel/validate/pluralApiName.js +3 -1
- package/crud/contentModel/validate/singularApiName.js +3 -1
- package/crud/contentModel/validateModel.js +3 -1
- package/crud/contentModel/validateModel.js.map +1 -1
- package/crud/contentModel/validateModelFields.js +3 -1
- package/crud/contentModel/validation.d.ts +339 -58
- package/crud/contentModel/validation.js +71 -11
- package/crud/contentModel/validation.js.map +1 -1
- package/crud/contentModel.crud.js +80 -156
- package/crud/contentModel.crud.js.map +1 -1
- package/crud/contentModelGroup/beforeCreate.d.ts +1 -1
- package/crud/contentModelGroup/beforeCreate.js +15 -1
- package/crud/contentModelGroup/beforeCreate.js.map +1 -1
- package/crud/contentModelGroup/beforeDelete.js +3 -1
- package/crud/contentModelGroup/beforeUpdate.js +3 -1
- package/crud/contentModelGroup/listGroupsFromDatabase.d.ts +8 -0
- package/crud/contentModelGroup/listGroupsFromDatabase.js +22 -0
- package/crud/contentModelGroup/listGroupsFromDatabase.js.map +1 -0
- package/crud/contentModelGroup/validation.d.ts +9 -6
- package/crud/contentModelGroup/validation.js +5 -2
- package/crud/contentModelGroup/validation.js.map +1 -1
- package/crud/contentModelGroup.crud.js +89 -110
- package/crud/contentModelGroup.crud.js.map +1 -1
- package/crud/system.crud.js +4 -7
- package/crud/system.crud.js.map +1 -1
- package/export/crud/exporting.d.ts +3 -0
- package/export/crud/exporting.js +50 -0
- package/export/crud/exporting.js.map +1 -0
- package/export/crud/importing.d.ts +3 -0
- package/export/crud/importing.js +79 -0
- package/export/crud/importing.js.map +1 -0
- package/export/crud/imports/importData.d.ts +14 -0
- package/export/crud/imports/importData.js +25 -0
- package/export/crud/imports/importData.js.map +1 -0
- package/export/crud/imports/importGroups.d.ts +8 -0
- package/export/crud/imports/importGroups.js +99 -0
- package/export/crud/imports/importGroups.js.map +1 -0
- package/export/crud/imports/importModels.d.ts +8 -0
- package/export/crud/imports/importModels.js +134 -0
- package/export/crud/imports/importModels.js.map +1 -0
- package/export/crud/imports/validateGroups.d.ts +8 -0
- package/export/crud/imports/validateGroups.js +106 -0
- package/export/crud/imports/validateGroups.js.map +1 -0
- package/export/crud/imports/validateInput.d.ts +19 -0
- package/export/crud/imports/validateInput.js +49 -0
- package/export/crud/imports/validateInput.js.map +1 -0
- package/export/crud/imports/validateModels.d.ts +9 -0
- package/export/crud/imports/validateModels.js +204 -0
- package/export/crud/imports/validateModels.js.map +1 -0
- package/export/crud/index.d.ts +4 -0
- package/export/crud/index.js +15 -0
- package/export/crud/index.js.map +1 -0
- package/export/crud/sanitize.d.ts +4 -0
- package/export/crud/sanitize.js +35 -0
- package/export/crud/sanitize.js.map +1 -0
- package/export/graphql/index.d.ts +3 -0
- package/export/graphql/index.js +188 -0
- package/export/graphql/index.js.map +1 -0
- package/export/index.d.ts +2 -0
- package/export/index.js +29 -0
- package/export/index.js.map +1 -0
- package/export/types.d.ts +111 -0
- package/export/types.js +22 -0
- package/export/types.js.map +1 -0
- package/fieldConverters/CmsModelDefaultFieldConverterPlugin.js +3 -1
- package/fieldConverters/CmsModelDynamicZoneFieldConverterPlugin.js +3 -1
- package/fieldConverters/CmsModelObjectFieldConverterPlugin.js +3 -1
- package/fieldConverters/index.js +3 -1
- package/graphql/buildSchemaPlugins.js +3 -1
- package/graphql/checkEndpointAccess.js +3 -1
- package/graphql/createExecutableSchema.js +3 -1
- package/graphql/createRequestBody.js +3 -1
- package/graphql/formatErrorPayload.js +3 -1
- package/graphql/generateSchema.js +3 -1
- package/graphql/getSchema.d.ts +0 -1
- package/graphql/getSchema.js +23 -13
- package/graphql/getSchema.js.map +1 -1
- package/graphql/graphQLHandlerFactory.js +3 -1
- package/graphql/handleRequest.js +3 -5
- package/graphql/handleRequest.js.map +1 -1
- package/graphql/index.d.ts +1 -1
- package/graphql/index.js +4 -2
- package/graphql/index.js.map +1 -1
- package/graphql/schema/baseContentSchema.js +3 -1
- package/graphql/schema/baseSchema.d.ts +2 -3
- package/graphql/schema/baseSchema.js +58 -2
- package/graphql/schema/baseSchema.js.map +1 -1
- package/graphql/schema/contentEntries.js +4 -2
- package/graphql/schema/contentEntries.js.map +1 -1
- package/graphql/schema/contentModelGroups.js +4 -1
- package/graphql/schema/contentModelGroups.js.map +1 -1
- package/graphql/schema/contentModels.js +5 -5
- package/graphql/schema/contentModels.js.map +1 -1
- package/graphql/schema/createFieldResolvers.js +3 -1
- package/graphql/schema/createFieldTypePluginRecords.js +3 -1
- package/graphql/schema/createManageResolvers.js +7 -1
- package/graphql/schema/createManageResolvers.js.map +1 -1
- package/graphql/schema/createManageSDL.js +8 -4
- package/graphql/schema/createManageSDL.js.map +1 -1
- package/graphql/schema/createPreviewResolvers.js +3 -1
- package/graphql/schema/createReadResolvers.js +3 -1
- package/graphql/schema/createReadSDL.js +3 -1
- package/graphql/schema/resolvers/commonFieldResolvers.js +3 -1
- package/graphql/schema/resolvers/manage/resolveCreate.d.ts +2 -1
- package/graphql/schema/resolvers/manage/resolveCreate.js +4 -2
- package/graphql/schema/resolvers/manage/resolveCreate.js.map +1 -1
- package/graphql/schema/resolvers/manage/resolveCreateFrom.d.ts +2 -1
- package/graphql/schema/resolvers/manage/resolveCreateFrom.js +4 -2
- package/graphql/schema/resolvers/manage/resolveCreateFrom.js.map +1 -1
- package/graphql/schema/resolvers/manage/resolveDelete.js +3 -1
- package/graphql/schema/resolvers/manage/resolveDeleteMultiple.js +3 -1
- package/graphql/schema/resolvers/manage/resolveGet.js +3 -1
- package/graphql/schema/resolvers/manage/resolveGetByIds.js +3 -1
- package/graphql/schema/resolvers/manage/resolveGetRevisions.js +3 -1
- package/graphql/schema/resolvers/manage/resolveGetUniqueFieldValues.js +3 -1
- package/graphql/schema/resolvers/manage/resolveList.js +3 -1
- package/graphql/schema/resolvers/manage/resolveMove.js +3 -1
- package/graphql/schema/resolvers/manage/resolvePublish.js +3 -1
- package/graphql/schema/resolvers/manage/resolveRepublish.js +3 -1
- package/graphql/schema/resolvers/manage/resolveUnpublish.js +3 -1
- package/graphql/schema/resolvers/manage/resolveUpdate.d.ts +2 -1
- package/graphql/schema/resolvers/manage/resolveUpdate.js +4 -2
- package/graphql/schema/resolvers/manage/resolveUpdate.js.map +1 -1
- package/graphql/schema/resolvers/manage/resolveValidate.d.ts +8 -0
- package/graphql/schema/resolvers/manage/resolveValidate.js +20 -0
- package/graphql/schema/resolvers/manage/resolveValidate.js.map +1 -0
- package/graphql/schema/resolvers/preview/resolveGet.js +3 -1
- package/graphql/schema/resolvers/preview/resolveList.js +3 -1
- package/graphql/schema/resolvers/read/resolveGet.js +3 -1
- package/graphql/schema/resolvers/read/resolveList.js +3 -1
- package/graphql/schema/schemaPlugins.js +3 -1
- package/graphql/system.js +3 -1
- package/graphqlFields/boolean.js +3 -1
- package/graphqlFields/datetime.js +4 -3
- package/graphqlFields/datetime.js.map +1 -1
- package/graphqlFields/dynamicZone/dynamicZoneField.js +29 -6
- package/graphqlFields/dynamicZone/dynamicZoneField.js.map +1 -1
- package/graphqlFields/dynamicZone/dynamicZoneStorage.js +3 -1
- package/graphqlFields/dynamicZone/index.js +3 -1
- package/graphqlFields/file.js +3 -1
- package/graphqlFields/helpers.d.ts +0 -1
- package/graphqlFields/helpers.js +6 -18
- package/graphqlFields/helpers.js.map +1 -1
- package/graphqlFields/index.js +3 -1
- package/graphqlFields/longText.js +14 -3
- package/graphqlFields/longText.js.map +1 -1
- package/graphqlFields/number.js +3 -1
- package/graphqlFields/object.js +5 -17
- package/graphqlFields/object.js.map +1 -1
- package/graphqlFields/ref.js +4 -2
- package/graphqlFields/ref.js.map +1 -1
- package/graphqlFields/richText.js +3 -1
- package/graphqlFields/text.js +3 -1
- package/index.d.ts +1 -1
- package/index.js +5 -2
- package/index.js.map +1 -1
- package/modelManager/DefaultCmsModelManager.js +3 -1
- package/modelManager/index.js +3 -1
- package/package.json +18 -19
- package/parameters/context.js +3 -1
- package/parameters/header.js +3 -1
- package/parameters/index.js +3 -1
- package/parameters/manual.js +3 -1
- package/parameters/path.js +3 -1
- package/plugins/CmsGraphQLSchemaPlugin.js +3 -1
- package/plugins/CmsGraphQLSchemaSorterPlugin.js +3 -1
- package/plugins/CmsGroupPlugin.d.ts +6 -3
- package/plugins/CmsGroupPlugin.js +7 -2
- package/plugins/CmsGroupPlugin.js.map +1 -1
- package/plugins/CmsModelFieldConverterPlugin.js +3 -1
- package/plugins/CmsModelPlugin.d.ts +2 -2
- package/plugins/CmsModelPlugin.js +6 -2
- package/plugins/CmsModelPlugin.js.map +1 -1
- package/plugins/CmsParametersPlugin.js +3 -1
- package/plugins/StorageOperationsCmsModelPlugin.js +3 -1
- package/plugins/StorageTransformPlugin.js +3 -1
- package/plugins/index.js +3 -1
- package/storage/default.js +3 -1
- package/storage/object.js +3 -1
- package/types.d.ts +62 -230
- package/types.js +3 -17
- package/types.js.map +1 -1
- package/utils/access.js +3 -1
- package/utils/caching/Cache.d.ts +2 -0
- package/utils/caching/Cache.js +53 -0
- package/utils/caching/Cache.js.map +1 -0
- package/utils/caching/CacheKey.d.ts +3 -0
- package/utils/caching/CacheKey.js +41 -0
- package/utils/caching/CacheKey.js.map +1 -0
- package/utils/caching/index.d.ts +2 -0
- package/utils/caching/index.js +29 -0
- package/utils/caching/index.js.map +1 -0
- package/utils/caching/types.d.ts +9 -0
- package/utils/caching/types.js +7 -0
- package/utils/caching/types.js.map +1 -0
- package/utils/converters/Converter.js +3 -1
- package/utils/converters/ConverterCollection.js +3 -1
- package/utils/converters/valueKeyStorageConverter.js +3 -1
- package/utils/createTypeFromFields.js +6 -1
- package/utils/createTypeFromFields.js.map +1 -1
- package/utils/createTypeName.js +3 -1
- package/utils/entryStorage.js +3 -1
- package/utils/filterAsync.js +3 -1
- package/utils/getBaseFieldType.js +3 -1
- package/utils/getEntryDescription.js +3 -1
- package/utils/getEntryImage.js +3 -1
- package/utils/getEntryTitle.js +3 -1
- package/utils/getSchemaFromFieldPlugins.js +3 -1
- package/utils/incrementEntryIdVersion.js +3 -1
- package/utils/index.d.ts +1 -0
- package/utils/index.js +18 -0
- package/utils/index.js.map +1 -0
- package/utils/permissions/EntriesPermissions.js +3 -1
- package/utils/permissions/ModelGroupsPermissions.js +3 -1
- package/utils/permissions/ModelsPermissions.js +3 -1
- package/utils/renderFields.js +3 -1
- package/utils/renderGetFilterFields.js +3 -1
- package/utils/renderInputFields.js +3 -1
- package/utils/renderListFilterFields.js +3 -1
- package/utils/renderSortEnum.js +3 -1
- package/utils/toSlug.js +3 -1
- package/validators/dateGte.js +3 -1
- package/validators/dateLte.js +3 -1
- package/validators/gte.js +3 -1
- package/validators/in.js +3 -1
- package/validators/index.js +4 -3
- package/validators/index.js.map +1 -1
- package/validators/lte.js +3 -1
- package/validators/maxLength.js +3 -1
- package/validators/minLength.js +3 -1
- package/validators/pattern.js +3 -1
- package/validators/patternPlugins/email.js +3 -1
- package/validators/patternPlugins/index.js +3 -1
- package/validators/patternPlugins/lowerCase.js +3 -1
- package/validators/patternPlugins/lowerCaseSpace.js +3 -1
- package/validators/patternPlugins/upperCase.js +3 -1
- package/validators/patternPlugins/upperCaseSpace.js +3 -1
- package/validators/patternPlugins/url.js +3 -1
- package/validators/required.js +3 -1
- package/validators/timeGte.js +3 -1
- package/validators/timeLte.js +3 -1
- package/validators/unique.js +4 -2
- package/validators/unique.js.map +1 -1
- package/crud/contentModel/afterCreate.d.ts +0 -8
- package/crud/contentModel/afterCreate.js +0 -16
- package/crud/contentModel/afterCreate.js.map +0 -1
- package/crud/contentModel/afterCreateFrom.d.ts +0 -8
- package/crud/contentModel/afterCreateFrom.js +0 -16
- package/crud/contentModel/afterCreateFrom.js.map +0 -1
- package/crud/contentModel/afterDelete.d.ts +0 -8
- package/crud/contentModel/afterDelete.js +0 -16
- package/crud/contentModel/afterDelete.js.map +0 -1
- package/crud/contentModel/afterUpdate.d.ts +0 -8
- package/crud/contentModel/afterUpdate.js +0 -16
- package/crud/contentModel/afterUpdate.js.map +0 -1
- package/crud/contentModel/validateLayout.d.ts +0 -2
- package/crud/contentModel/validateLayout.js +0 -28
- package/crud/contentModel/validateLayout.js.map +0 -1
- package/crud/settings.crud.d.ts +0 -12
- package/crud/settings.crud.js +0 -62
- package/crud/settings.crud.js.map +0 -1
- package/utils/permissions/SettingsPermissions.d.ts +0 -4
- package/utils/permissions/SettingsPermissions.js +0 -9
- package/utils/permissions/SettingsPermissions.js.map +0 -1
- package/validators/dynamicZone.d.ts +0 -2
- package/validators/dynamicZone.js +0 -20
- 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)
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
|
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
|
|
170
|
+
if (!referenceFieldPaths.length) {
|
|
152
171
|
return output;
|
|
153
172
|
}
|
|
154
|
-
|
|
155
|
-
|
|
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
|
|
166
|
-
referencesByModel
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
264
|
+
return context.cms.getEntriesByIds(model, referencesByModel.get(model.modelId) || []);
|
|
206
265
|
});
|
|
207
266
|
});
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
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
|
|
275
|
+
* Verify that all entries exist.
|
|
221
276
|
*/
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
for (const id of
|
|
225
|
-
if (
|
|
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;EAC7E,MAAM;IAAEC,MAAM;IAAEC,WAAW,EAAEC,kBAAkB;IAAEC;EAAM,CAAC,GAAGJ,MAAM;EAEjE,MAAME,WAAW,GAAG,CAAC,GAAGC,kBAAkB,CAAC;EAE3C,MAAME,gBAAgB,GAAGC,KAAK,CAACC,OAAO,CAACH,KAAK,CAAC;EAE7C,OAAOH,MAAM,CACRO,MAAM,CAACC,KAAK,IAAI,CAAC,QAAQ,EAAE,KAAK,EAAE,aAAa,CAA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,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"],"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;AA8BA,MAAMI,wBAAwB,GAAIC,MAAgC,IAAe;EAC7E,MAAM;IAAEC,MAAM;IAAEC,WAAW,EAAEC,kBAAkB;IAAEC;EAAM,CAAC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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;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;IAC9C;AACR;AACA;IACQ,MAAMC,MAAM,
|
|
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,IAAI,CAACI,MAAM,CAACQ,cAAc,IAAI,EAAAT,gBAAA,GAAAF,KAAK,CAACM,QAAQ,cAAAJ,gBAAA,uBAAdA,gBAAA,CAAgBU,qBAAqB,MAAK,IAAI,EAAE;MAC1E,OAAOb,MAAM;IACjB;;IAEA;AACR;AACA;IACQ,MAAMc,IAAI,GAAG,CAAC,GAAGhB,OAAO,EAAEG,KAAK,CAACS,OAAO,CAAC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