UNPKG

dynamodb-toolbox

Version:

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

63 lines (62 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.refOrValueTokens = exports.valueToken = exports.pathTokens = void 0; const index_js_1 = require("../../../../../errors/index.js"); const finder_js_1 = require("../../../../../schema/actions/finder/finder.js"); const isNumber_js_1 = require("../../../../../utils/validation/isNumber.js"); const index_js_2 = require("../../symbols/index.js"); const pathTokens = (path, prefix, state) => { let tokens = ''; path.arrayPath.forEach((pathPart, index) => { if ((0, isNumber_js_1.isNumber)(pathPart)) { tokens += `[${pathPart}]`; return; } let token = state.tokens[prefix][pathPart]; if (token === undefined) { token = `#${prefix}_${state.nameCursors[prefix]}`; state.tokens[prefix][pathPart] = token; state.ExpressionAttributeNames[token] = pathPart; state.nameCursors[prefix]++; } if (index > 0) { tokens += '.'; } tokens += token; }); return tokens; }; exports.pathTokens = pathTokens; const valueToken = (value, prefix, state) => { const token = `:${prefix}_${state.valueCursors[prefix]}`; state.ExpressionAttributeValues[token] = value; state.valueCursors[prefix]++; return token; }; exports.valueToken = valueToken; const refOrValueTokens = (refOrValue, prefix, state) => { if ((0, index_js_2.isGetting)(refOrValue)) { // TODO: Fix this cast const [reference, fallback] = refOrValue[index_js_2.$GET]; const [firstMatchingSubSchema] = new finder_js_1.Finder(state.rootSchema).search(reference); if (firstMatchingSubSchema === undefined) { throw new index_js_1.DynamoDBToolboxError('actions.invalidExpressionAttributePath', { message: `Unable to match update reference with schema: ${reference}`, payload: { attributePath: reference } }); } if (fallback === undefined) { return (0, exports.pathTokens)(firstMatchingSubSchema.transformedPath, prefix, state); } else { let Expression = 'if_not_exists('; Expression += (0, exports.pathTokens)(firstMatchingSubSchema.transformedPath, prefix, state); Expression += ', '; Expression += (0, exports.refOrValueTokens)(fallback, prefix, state); Expression += ')'; return Expression; } } return (0, exports.valueToken)(refOrValue, prefix, state); }; exports.refOrValueTokens = refOrValueTokens;