UNPKG

@nerdware/ddb-single-table

Version:

A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡

72 lines (71 loc) 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNormalizedComparisonMeta = void 0; const index_js_1 = require("../../utils/index.js"); const assertIsValidWhereQueryComparand_js_1 = require("./assertIsValidWhereQueryComparand.js"); const isValidWhereQueryOperator_js_1 = require("./isValidWhereQueryOperator.js"); /** * This function takes a key-value pair from an {@link ItemWhereQuery} object, * validates the values it contains, and if they're valid, returns the appropriate * comparison `operator` and `comparand` to use in the `KeyConditionExpression`. * * @returns An {@link ComparisonMetaObject} containing the `operator` and `comparand` for the given `WhereQuery` comparison. * @throws {InvalidExpressionError} if the args are invalid. */ const getNormalizedComparisonMeta = (attrName, whereQueryComparison) => { // Determine the operator and comparand to use in the expression let returnedComparisonMeta; // First check if the comparison is an object if (!index_js_1.isType.map(whereQueryComparison)) { // If whereQueryComparison is not an object, ensure it's a string or number (short-hand "eq") if (!index_js_1.isType.string(whereQueryComparison) && !index_js_1.isType.number(whereQueryComparison)) { throw new index_js_1.InvalidExpressionError({ expressionName: "KeyConditionExpression", invalidValue: whereQueryComparison, invalidValueDescription: "WhereQuery value", problem: `WhereQuery value for attribute "${attrName}" contains an invalid value for short-hand "eq" expressions`, }); } // If it's a string or number, it's a short-hand "eq", and the comparand is the whereQueryComparison value returnedComparisonMeta = { operator: "eq", comparand: whereQueryComparison, }; } else { // If the value IS an object, ensure it's a valid WhereQuery comparison object. // First, ensure the object contains exactly one K-V pair. const comparisonOperatorKeys = Object.keys(whereQueryComparison); if (comparisonOperatorKeys.length !== 1) { throw new index_js_1.InvalidExpressionError({ expressionName: "KeyConditionExpression", invalidValue: whereQueryComparison, invalidValueDescription: "WhereQuery comparison object", problem: `KeyConditionExpressions can only include one logical operator per key, but the ` + `WhereQuery object for attribute "${attrName}" contains more than one operator`, }); } // Extract the K-V pair from the lone entry const comparisonOperator = comparisonOperatorKeys[0]; // Ensure the key is a valid operator if (!(0, isValidWhereQueryOperator_js_1.isValidWhereQueryOperator)(comparisonOperator)) { throw new index_js_1.InvalidExpressionError({ expressionName: "KeyConditionExpression", invalidValue: comparisonOperator, invalidValueDescription: "WhereQuery comparison operator", problem: `An invalid comparison operator was provided for attribute "${attrName}"`, }); } const comparisonComparand = whereQueryComparison[comparisonOperator]; // Ensure the value is a valid comparand for the operator (0, assertIsValidWhereQueryComparand_js_1.assertIsValidWhereQueryComparand)(comparisonOperator, comparisonComparand); // If we've made it this far, the operator and comparand are valid. returnedComparisonMeta = { operator: comparisonOperator, comparand: comparisonComparand, }; } // Return the comparison-meta object return returnedComparisonMeta; }; exports.getNormalizedComparisonMeta = getNormalizedComparisonMeta;