@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
190 lines (183 loc) • 5.67 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.prepareEntryToIndex = exports.extractEntriesFromIndex = void 0;
var _error = _interopRequireDefault(require("@webiny/error"));
var _fieldIdentifier = require("./fieldIdentifier");
const prepareEntryToIndex = params => {
const {
plugins,
storageEntry,
entry,
model
} = params;
const {
fieldIndexPlugins,
defaultIndexFieldPlugin,
fieldTypePlugins
} = setupEntriesIndexHelpers({
plugins
});
function getFieldIndexPlugin(fieldType) {
return fieldIndexPlugins[fieldType] || defaultIndexFieldPlugin;
}
function getFieldTypePlugin(fieldType) {
const pl = fieldTypePlugins[fieldType];
if (pl) {
return pl;
}
throw new _error.default(`Missing field type plugin "${fieldType}". Prepare entry for index.`);
}
// These objects will contain values processed by field index plugins
const values = {};
const rawValues = {};
// We're only interested in current model fields.
for (const field of model.fields) {
const identifier = (0, _fieldIdentifier.getFieldIdentifier)(storageEntry.values, field);
if (!identifier) {
continue;
}
const targetFieldPlugin = getFieldIndexPlugin(field.type);
// TODO: remove this `if` once we convert this plugin to proper plugin class
if (!targetFieldPlugin || !targetFieldPlugin.toIndex) {
continue;
}
const {
value,
rawValue
} = targetFieldPlugin.toIndex({
plugins,
model,
field,
rawValue: entry.values[identifier],
value: storageEntry.values[identifier],
getFieldIndexPlugin,
getFieldTypePlugin
});
if (typeof value !== "undefined") {
values[identifier] = value;
}
if (typeof rawValue !== "undefined") {
rawValues[identifier] = rawValue;
}
}
return {
...storageEntry,
values,
rawValues
};
};
exports.prepareEntryToIndex = prepareEntryToIndex;
const setupEntriesIndexHelpers = ({
plugins: pluginsContainer
}) => {
const plugins = pluginsContainer.byType("cms-model-field-to-elastic-search");
const fieldIndexPlugins = {};
for (const plugin of plugins.reverse()) {
if (fieldIndexPlugins[plugin.fieldType]) {
continue;
}
fieldIndexPlugins[plugin.fieldType] = plugin;
}
// we will use this plugin if no targeted plugin found
const defaultIndexFieldPlugin = plugins.find(plugin => plugin.fieldType === "*");
// CmsModelFieldToGraphQLPlugin plugins
const fieldTypePlugins = pluginsContainer.byType("cms-model-field-to-graphql").reduce((plugins, plugin) => ({
...plugins,
[plugin.fieldType]: plugin
}), {});
return {
fieldIndexPlugins,
defaultIndexFieldPlugin,
fieldTypePlugins
};
};
const extractEntriesFromIndex = ({
plugins,
entries,
model
}) => {
const {
fieldIndexPlugins,
defaultIndexFieldPlugin,
fieldTypePlugins
} = setupEntriesIndexHelpers({
plugins
});
function getFieldIndexPlugin(fieldType) {
return fieldIndexPlugins[fieldType] || defaultIndexFieldPlugin;
}
function getFieldTypePlugin(fieldType) {
return fieldTypePlugins[fieldType];
}
const list = [];
for (const entry of entries) {
// This object will contain values processed by field index plugins
const indexValues = {};
// We only consider fields that are present in the model
for (const field of model.fields) {
const fieldTypePlugin = fieldTypePlugins[field.type];
if (!fieldTypePlugin) {
throw new _error.default(`Missing field type plugin "${field.type}". Extract entries from index.`);
}
const targetFieldPlugin = getFieldIndexPlugin(field.type);
if (!targetFieldPlugin || !targetFieldPlugin.fromIndex) {
continue;
}
/**
* We can safely cast as the code will not continue in case of no identifiers.
*/
const identifiers = (0, _fieldIdentifier.getFieldIdentifiers)(entry.values, entry.rawValues, field);
if (!identifiers) {
continue;
}
try {
indexValues[identifiers.valueIdentifier] = targetFieldPlugin.fromIndex({
plugins,
model,
field,
getFieldIndexPlugin,
getFieldTypePlugin,
value: entry.values[identifiers.valueIdentifier || identifiers.rawValueIdentifier],
/**
* Possibly no rawValues so we must check for the existence of the field.
*/
rawValue: entry.rawValues ? entry.rawValues[identifiers.rawValueIdentifier || identifiers.valueIdentifier] : null
});
} catch (ex) {
throw new _error.default(ex.message || "Could not transform entry field from index.", ex.code || "FIELD_FROM_INDEX_ERROR", {
field,
entry
});
}
}
/**
* Let's have a new entry so we do not modify the original one.
*/
const newEntry = {
...entry,
values: indexValues
};
/**
* If we want to remove the rawValues, TYPE, latest, published and __type, we must make them optional or ignore them.
*/
// @ts-expect-error
delete newEntry["rawValues"];
// @ts-expect-error
delete newEntry["TYPE"];
// @ts-expect-error
delete newEntry["__type"];
// @ts-expect-error
delete newEntry["latest"];
// @ts-expect-error
delete newEntry["published"];
list.push({
...newEntry
});
}
return list;
};
exports.extractEntriesFromIndex = extractEntriesFromIndex;
//# sourceMappingURL=entryIndexHelpers.js.map