UNPKG

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

Version:

DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.

87 lines (85 loc) 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createElasticsearchSort = void 0; var _apiElasticsearch = require("@webiny/api-elasticsearch"); var _keyword = require("./keyword"); var _search = require("./plugins/search"); var _path = require("./filtering/path"); const createElasticsearchSort = params => { const { sort, modelFields, plugins } = params; if (!sort || sort.length === 0) { return [{ ["id.keyword"]: { order: "asc" } }]; } const searchPlugins = (0, _search.createSearchPluginList)({ plugins }); const createFieldPath = (0, _path.createFieldPathFactory)({ plugins: searchPlugins }); const fieldIdToStorageIdIdMap = {}; const sortPlugins = Object.values(modelFields).reduce((plugins, field) => { /** * We do not support sorting by nested fields. */ if (field.parents.length > 0) { return plugins; } const { fieldId, storageId } = field.field; fieldIdToStorageIdIdMap[fieldId] = fieldId; const { path } = createFieldPath({ key: storageId, field, value: "", keyword: false }); /** * Plugins must be stored with fieldId as key because it is later used to find the sorting plugin. */ plugins[fieldId] = new _apiElasticsearch.ElasticsearchFieldPlugin({ unmappedType: field.unmappedType, keyword: (0, _keyword.hasKeyword)(field), sortable: field.sortable, searchable: field.searchable, field: fieldId, path }); return plugins; }, { ["*"]: new _apiElasticsearch.ElasticsearchFieldPlugin({ field: _apiElasticsearch.ElasticsearchFieldPlugin.ALL, keyword: false }) }); const transformedSort = sort.map(value => { const matched = value.match(/^([a-zA-Z-0-9_]+)_(ASC|DESC)$/); if (!matched) { return null; } const [, fieldId, order] = matched; if (fieldIdToStorageIdIdMap[fieldId]) { return `${fieldIdToStorageIdIdMap[fieldId]}_${order}`; } return value; }).filter(Boolean); return (0, _apiElasticsearch.createSort)({ fieldPlugins: sortPlugins, sort: transformedSort }); }; exports.createElasticsearchSort = createElasticsearchSort; //# sourceMappingURL=sort.js.map