UNPKG

@webiny/api-headless-cms-ddb

Version:

DynamoDB storage operations plugin for Headless CMS API.

178 lines (174 loc) 5.31 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createExpressions = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin"); var _mapPlugins = require("./mapPlugins"); var _where = require("./where"); var _transform = require("./transform"); var _CmsEntryFieldFilterPlugin = require("../../../plugins/CmsEntryFieldFilterPlugin"); var _values = require("./values"); const createExpressions = params => { const { where, plugins, fields } = params; const filterPlugins = (0, _mapPlugins.getMappedPlugins)({ plugins, type: _ValueFilterPlugin.ValueFilterPlugin.type, property: "operation" }); const transformValuePlugins = (0, _mapPlugins.getMappedPlugins)({ plugins, type: "cms-field-filter-value-transform", property: "fieldType" }); const fieldFilterCreatePlugins = (0, _mapPlugins.getMappedPlugins)({ plugins, type: _CmsEntryFieldFilterPlugin.CmsEntryFieldFilterPlugin.type, property: "fieldType" }); const defaultFilterCreatePlugin = fieldFilterCreatePlugins["*"]; const getFilterCreatePlugin = type => { const filterCreatePlugin = fieldFilterCreatePlugins[type] || defaultFilterCreatePlugin; if (filterCreatePlugin) { return filterCreatePlugin; } throw new _error.default(`There is no filter create plugin for the field type "${type}".`, "MISSING_FILTER_CREATE_PLUGIN", { type }); }; const createExpression = ({ where, condition }) => { const expression = { filters: [], expressions: [], condition }; for (const key in where) { const value = where[key]; if (value === undefined) { continue; } /** * If there are "AND" or "OR" keys, let's sort them out first. * * * AND conditional */ if (key === "AND") { const childWhereList = (0, _values.getWhereValues)(value, key); const childExpression = { condition: "AND", filters: [], expressions: [] }; for (const childWhere of childWhereList) { const result = createExpression({ where: childWhere, condition: "AND" }); childExpression.expressions.push(result); } expression.expressions.push(childExpression); continue; } /** * OR conditional */ if (key === "OR") { const childWhereList = (0, _values.getWhereValues)(value, key); const childExpression = { condition: "OR", filters: [], expressions: [] }; for (const childWhere of childWhereList) { const result = createExpression({ where: childWhere, condition: "AND" }); childExpression.expressions.push(result); } expression.expressions.push(childExpression); continue; } const whereParams = (0, _where.extractWhereParams)(key); if (!whereParams) { continue; } const { fieldId, operation, negate } = whereParams; const field = fields[fieldId]; if (!field) { throw new _error.default(`There is no field with the fieldId "${fieldId}".`, "FIELD_ERROR", { fieldId }); } /** * We need a filter create plugin for this type. */ const filterCreatePlugin = getFilterCreatePlugin(field.type); const transformValuePlugin = transformValuePlugins[field.type]; const transformValueCallable = value => { if (!transformValuePlugin) { return value; } return transformValuePlugin.transform({ field, value }); }; const result = filterCreatePlugin.create({ key, value, valueFilterPlugins: filterPlugins, transformValuePlugins, getFilterCreatePlugin, operation, negate, field, fields, compareValue: (0, _transform.transformValue)({ value, transform: transformValueCallable }), transformValue: transformValueCallable }); /** * There is a possibility of * - no result * - result being an array * - result being an object */ if (!result || Array.isArray(result) && result.length === 0) { continue; } expression.filters.push(...(Array.isArray(result) ? result : [result])); } return expression; }; const expression = createExpression({ where, condition: "AND" }); /** * If the first expression has no filters and has only one expression, put that expression as main one. * This will mostly be used when having an OR condition as the single expression in the root level of the where */ if (expression.filters.length > 0 || expression.expressions.length !== 1) { return expression; } return expression.expressions[0]; }; exports.createExpressions = createExpressions; //# sourceMappingURL=createExpressions.js.map