@webiny/api-headless-cms-ddb-es
Version:
DynamoDB and Elasticsearch storage operations plugin for Headless CMS API.
125 lines (124 loc) • 3.61 kB
JavaScript
import WebinyError from "@webiny/error";
import { createOperatorPluginList } from "../plugins/operator.js";
import { createBaseQuery } from "../initialQuery.js";
import { parseWhereKey } from "@webiny/api-opensearch";
import { getWhereValues } from "./values.js";
import { getPopulated } from "./populated.js";
import { createApplyFiltering } from "./applyFiltering.js";
import { assignMinimumShouldMatchToQuery } from "../assignMinimumShouldMatchToQuery.js";
export const createExecFiltering = params => {
const {
fields,
plugins,
model,
valueSearchRegistry,
filterRegistry
} = params;
const operatorPlugins = createOperatorPluginList({
plugins
});
const applyFiltering = createApplyFiltering({
operatorPlugins,
valueSearchRegistry
});
const getFilter = type => {
return filterRegistry.get(type);
};
const execFiltering = params => {
const {
where: initialWhere,
query,
isValues = false
} = params;
const keys = Object.keys(initialWhere);
if (keys.length === 0) {
return;
}
const where = structuredClone(initialWhere);
for (const key in where) {
const value = where[key];
if (value === undefined) {
continue;
} else if (key === "AND") {
const childWhereList = getWhereValues(value, "AND");
const childQuery = createBaseQuery();
for (const childWhere of childWhereList) {
execFiltering({
query: childQuery,
where: childWhere,
isValues
});
}
const childQueryBool = getPopulated(childQuery);
if (Object.keys(childQueryBool).length === 0) {
continue;
}
query.filter.push({
bool: childQueryBool
});
continue;
} else if (key === "OR") {
const childWhereList = getWhereValues(value, "OR");
const should = [];
for (const childWhere of childWhereList) {
const childQuery = createBaseQuery();
execFiltering({
query: childQuery,
where: childWhere,
isValues
});
const childQueryBool = getPopulated(childQuery);
if (Object.keys(childQueryBool).length === 0) {
continue;
}
should.push({
bool: childQueryBool
});
}
if (should.length === 0) {
continue;
}
query.should.push(...should);
assignMinimumShouldMatchToQuery({
query
});
continue;
} else if (key === "values") {
execFiltering({
query,
where: where[key],
isValues: true
});
continue;
}
const {
field: whereFieldId,
operator
} = parseWhereKey(key);
let fieldId = isValues ? `values.${whereFieldId}` : whereFieldId;
const cmsModelField = model.fields.find(f => f.fieldId === fieldId);
if (!cmsModelField && !fields[fieldId]) {
throw new WebinyError(`There is no CMS Model Field "${fieldId}".`);
} else if (cmsModelField) {
fieldId = cmsModelField.fieldId;
}
const field = fields[fieldId];
if (!field) {
throw new WebinyError(`There is no field "${fieldId}".`, "EXEC_FILTERING_ERROR");
}
const filter = getFilter(field.type);
filter.exec({
applyFiltering,
getFilter,
key,
value,
operator,
field,
fields,
query
});
}
};
return execFiltering;
};
//# sourceMappingURL=exec.js.map