@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
55 lines (54 loc) • 1.5 kB
JavaScript
import { CmsEntryOpenSearchValueSearch } from "../abstractions/CmsEntryOpenSearchValueSearch.js";
import { WebinyError } from "@webiny/error";
import { NoValueContainer } from "../../../values/NoValueContainer.js";
const getKey = params => {
const {
field,
value
} = params;
const keys = Object.keys(value);
if (keys.length === 0) {
throw new WebinyError(`Searchable JSON field "${field.fieldId}" cannot be empty.`, "EMPTY_SEARCHABLE_JSON_FIELD", {
field,
value
});
} else if (keys.length > 1) {
throw new WebinyError(`Searchable JSON field "${field.fieldId}" can only have one key.`, "MULTIPLE_KEYS_IN_SEARCHABLE_JSON_FIELD", {
field,
value,
keys
});
}
return keys[0];
};
class SearchableJsonSearchImpl {
fieldType = "searchable-json";
transform(params) {
const {
value
} = params;
if (NoValueContainer.is(value)) {
return null;
}
const key = getKey(params);
return value[key] || null;
}
createPath(params) {
const {
field
} = params;
if (NoValueContainer.is(params.originalValue)) {
return `${field.storageId}.unknown`;
}
const key = getKey({
field,
value: params.originalValue
});
return `${field.storageId}.${key}`;
}
}
export const SearchableJsonSearch = CmsEntryOpenSearchValueSearch.createImplementation({
implementation: SearchableJsonSearchImpl,
dependencies: []
});
//# sourceMappingURL=SearchableJsonSearch.js.map