UNPKG

@webiny/api-headless-cms-ddb-es

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

315 lines (313 loc) 7.91 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createModelFields = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _plugins = require("../../../plugins"); var _constants = require("@webiny/api-headless-cms/constants"); const createSystemField = field => { return { ...field, id: field.fieldId, label: field.fieldId }; }; const createSystemFields = () => { const onMetaFields = _constants.ENTRY_META_FIELDS.filter(_constants.isDateTimeEntryMetaField).reduce((current, fieldName) => { return { ...current, [fieldName]: { type: "date", unmappedType: "date", keyword: false, systemField: true, searchable: true, sortable: true, field: createSystemField({ storageId: fieldName, fieldId: fieldName, type: "text", settings: { type: "dateTimeWithoutTimezone" } }), parents: [] } }; }, {}); const byMetaFields = _constants.ENTRY_META_FIELDS.filter(_constants.isIdentityEntryMetaField).reduce((current, fieldName) => { return { ...current, [fieldName]: { type: "text", unmappedType: undefined, systemField: true, searchable: true, sortable: true, path: `${fieldName}.id`, field: createSystemField({ storageId: fieldName, fieldId: fieldName, type: "text" }), parents: [] } }; }, {}); return { id: { type: "text", systemField: true, searchable: true, sortable: true, field: createSystemField({ storageId: "id", fieldId: "id", type: "text" }), parents: [] }, entryId: { type: "text", systemField: true, searchable: true, sortable: true, field: createSystemField({ storageId: "entryId", fieldId: "entryId", type: "text" }), parents: [] }, ...onMetaFields, ...byMetaFields, wbyAco_location: { type: "object", systemField: true, searchable: true, sortable: true, field: createSystemField({ storageId: "location", fieldId: "wbyAco_location", type: "object", settings: { fields: [{ id: "folderId", fieldId: "folderId", storageId: "folderId", type: "text", label: "Folder ID" }] } }), parents: [] }, "wbyAco_location.folderId": { type: "text", systemField: true, searchable: true, sortable: true, field: createSystemField({ id: "folderId", fieldId: "folderId", storageId: "folderId", type: "text", label: "Folder ID" }), parents: [{ fieldId: "wbyAco_location", type: "object", storageId: "location" }] }, version: { type: "number", unmappedType: undefined, keyword: false, systemField: true, searchable: true, sortable: true, field: createSystemField({ storageId: "version", fieldId: "version", type: "number" }), parents: [] }, status: { type: "string", unmappedType: undefined, keyword: false, systemField: true, searchable: true, sortable: false, field: createSystemField({ storageId: "status", fieldId: "status", type: "string" }), parents: [] }, wbyDeleted: { type: "boolean", unmappedType: undefined, keyword: false, systemField: true, searchable: true, sortable: false, field: createSystemField({ storageId: "wbyDeleted", fieldId: "wbyDeleted", type: "boolean" }), parents: [] }, binOriginalFolderId: { type: "text", unmappedType: undefined, keyword: false, systemField: true, searchable: true, sortable: false, field: createSystemField({ storageId: "binOriginalFolderId", fieldId: "binOriginalFolderId", type: "text" }), parents: [] } }; }; const buildCustomFields = params => { const { fields, fieldTypePlugins } = params; return fields.reduce((collection, field) => { const typePlugin = fieldTypePlugins[field.fieldType]; if (!typePlugin) { return collection; } let unmappedType = undefined; if (typePlugin.unmappedType) { unmappedType = typePlugin.unmappedType(field); } collection[field.fieldId] = { type: field.fieldType, field: createSystemField({ storageId: field.fieldId, fieldId: field.fieldId, type: field.fieldType }), unmappedType, fullTextSearch: field.searchable ? typePlugin.fullTextSearch : false, searchable: field.searchable || typePlugin.searchable, sortable: field.sortable || typePlugin.sortable, systemField: false, path: field.path, parents: [] }; return collection; }, {}); }; const buildFieldsList = params => { const { plugins, fields, parents } = params; return fields.reduce((result, field) => { const plugin = plugins[field.type]; if (!plugin) { throw new _error.default(`There is no plugin for field type "${field.type}".`); } const { searchable, sortable, unmappedType, fullTextSearch } = plugin; /** * If a field has child fields, go through them and add them to a result. */ const childFields = field.settings?.fields || []; if (childFields.length > 0) { /** * Let's build all the child fields */ const childResult = buildFieldsList({ fields: childFields, plugins, parents: [...parents, { fieldId: field.fieldId, storageId: field.storageId, type: field.type }] }); Object.assign(result, childResult); } const identifier = [...parents.map(p => p.fieldId), field.fieldId].join("."); result[identifier] = { type: field.type, parents, searchable, sortable, fullTextSearch, unmappedType: typeof unmappedType === "function" ? unmappedType(field) : undefined, systemField: false, field }; return result; }, {}); }; const createModelFields = ({ plugins, model }) => { const fields = model.fields; const fieldDefinitionPlugins = plugins.byType(_plugins.CmsElasticsearchModelFieldPlugin.type).filter(plugin => { return plugin.canBeApplied(model.modelId); }); /** * Collect all unmappedType from elastic plugins. */ const unmappedTypes = plugins.byType("cms-model-field-to-elastic-search").reduce((acc, plugin) => { if (!plugin.unmappedType) { return acc; } acc[plugin.fieldType] = plugin.unmappedType; return acc; }, {}); /** * Collect all field types from the plugins. */ const fieldTypePlugins = plugins.byType("cms-model-field-to-graphql").reduce((types, plugin) => { const { fieldType, fullTextSearch } = plugin; types[fieldType] = { unmappedType: unmappedTypes[fieldType], searchable: plugin.isSearchable, sortable: plugin.isSortable, fullTextSearch }; return types; }, {}); return { ...createSystemFields(), ...buildCustomFields({ fields: fieldDefinitionPlugins, fieldTypePlugins }), ...buildFieldsList({ fields, plugins: fieldTypePlugins, parents: [] }) }; }; exports.createModelFields = createModelFields; //# sourceMappingURL=fields.js.map