@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
121 lines (120 loc) • 4.06 kB
JavaScript
import { transformEntryKeys } from "./transformEntryKeys.js";
import { transformEntryToIndex } from "./transformEntryToIndex.js";
import { modifyEntryValues as modifyEntryValuesCallable } from "./modifyEntryValues.js";
import { createLatestRecordType, createPublishedRecordType } from "../recordType.js";
import WebinyError from "@webiny/error";
export const createTransformer = params => {
const {
model,
fieldRegistry,
fieldIndexRegistry,
entry: baseEntry,
storageEntry: baseStorageEntry,
transformedToIndex: initialTransformedEntryToIndex = undefined,
compressionHandler,
valuesModifiers
} = params;
let transformedEntryKeys = undefined;
let transformedEntryToIndex = initialTransformedEntryToIndex;
let modifiedEntryValues = undefined;
let elasticsearchLatestEntry = undefined;
let elasticsearchPublishedEntry = undefined;
const applicableModifiers = valuesModifiers.filter(m => m.canModify(model.modelId));
const modifyEntryValues = () => {
if (initialTransformedEntryToIndex || !baseEntry) {
throw new WebinyError(`Should not call the "modifyEntryValues" when "transformedToIndex" is provided.`, "METHOD_NOT_ALLOWED", {
entry: initialTransformedEntryToIndex
});
}
if (modifiedEntryValues) {
return modifiedEntryValues;
}
const modifiedEntry = modifyEntryValuesCallable({
modifiers: applicableModifiers,
model,
entry: baseEntry
});
const modifiedStorageEntry = modifyEntryValuesCallable({
modifiers: applicableModifiers,
model,
entry: baseStorageEntry
});
return modifiedEntryValues = transformEntryKeys({
model,
entry: modifiedEntry,
storageEntry: modifiedStorageEntry
});
};
return {
transformEntryKeys: function () {
if (initialTransformedEntryToIndex || !baseEntry) {
throw new WebinyError(`Should not call the "modifyEntryValues" when "transformedToIndex" is provided.`, "METHOD_NOT_ALLOWED", {
entry: initialTransformedEntryToIndex
});
}
if (transformedEntryKeys) {
return transformedEntryKeys;
}
return transformedEntryKeys = transformEntryKeys({
model,
entry: baseEntry,
storageEntry: baseStorageEntry
});
},
transformToIndex: function () {
if (transformedEntryToIndex) {
return transformedEntryToIndex;
}
let entry;
let storageEntry;
/**
* In case there are value modifier plugins, we need to
* - run modifiers
* - transform keys
*/
if (applicableModifiers.length > 0) {
const result = modifyEntryValues();
entry = result.entry;
storageEntry = result.storageEntry;
}
// In case there are no modifier plugins, just transform the keys - or used already transformed.
else {
const result = this.transformEntryKeys();
entry = result.entry;
storageEntry = result.storageEntry;
}
return transformedEntryToIndex = transformEntryToIndex({
model,
entry,
storageEntry,
fieldRegistry,
fieldIndexRegistry
});
},
getElasticsearchLatestEntryData: async function () {
if (elasticsearchLatestEntry) {
return elasticsearchLatestEntry;
}
const entry = this.transformToIndex();
return elasticsearchLatestEntry = await compressionHandler.compress({
...entry,
latest: true,
TYPE: createLatestRecordType(),
__type: createLatestRecordType()
});
},
getElasticsearchPublishedEntryData: async function () {
if (elasticsearchPublishedEntry) {
return elasticsearchPublishedEntry;
}
const entry = this.transformToIndex();
return elasticsearchPublishedEntry = await compressionHandler.compress({
...entry,
published: true,
TYPE: createPublishedRecordType(),
__type: createPublishedRecordType()
});
}
};
};
//# sourceMappingURL=index.js.map