UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

50 lines (49 loc) 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.attrOrValueTokens = exports.valueToken = exports.pathTokens = void 0; const path_js_1 = require("../../../../../schema/actions/utils/path.js"); const isNumber_js_1 = require("../../../../../utils/validation/isNumber.js"); const isObject_js_1 = require("../../../../../utils/validation/isObject.js"); const pathTokens = (attr, prefix = '', state, size = false) => { let tokens = ''; new path_js_1.Path(attr).arrayPath.forEach((pathPart, index) => { if ((0, isNumber_js_1.isNumber)(pathPart)) { tokens += `[${pathPart}]`; return; } let token = state.tokens[pathPart]; if (token === undefined) { token = `#c${prefix}_${state.namesCursor}`; state.tokens[pathPart] = token; state.ExpressionAttributeNames[token] = pathPart; state.namesCursor++; } if (index > 0) { tokens += '.'; } tokens += token; }); if (size) { tokens = ['size(', tokens, ')'].join(''); } return tokens; }; exports.pathTokens = pathTokens; const valueToken = (value, prefix = '', state) => { const token = `:c${prefix}_${state.valuesCursor}`; state.ExpressionAttributeValues[token] = value; state.valuesCursor++; return token; }; exports.valueToken = valueToken; // NOTE: Simple object check is enough as objects are not valid condition values const isAttr = (attrOrValue) => (0, isObject_js_1.isObject)(attrOrValue); const attrOrValueTokens = (attrOrValue, prefix = '', state) => { if (isAttr(attrOrValue)) { return (0, exports.pathTokens)(attrOrValue.attr, prefix, state); } else { return (0, exports.valueToken)(attrOrValue, prefix, state); } }; exports.attrOrValueTokens = attrOrValueTokens;