UNPKG

dynamodb-toolbox

Version:

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

44 lines (43 loc) 1.53 kB
import { Path } from '../../../../../schema/actions/utils/path.js'; import { isNumber } from '../../../../../utils/validation/isNumber.js'; import { isObject } from '../../../../../utils/validation/isObject.js'; export const pathTokens = (attr, prefix = '', state, size = false) => { let tokens = ''; new Path(attr).arrayPath.forEach((pathPart, index) => { if (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; }; export const valueToken = (value, prefix = '', state) => { const token = `:c${prefix}_${state.valuesCursor}`; state.ExpressionAttributeValues[token] = value; state.valuesCursor++; return token; }; // NOTE: Simple object check is enough as objects are not valid condition values const isAttr = (attrOrValue) => isObject(attrOrValue); export const attrOrValueTokens = (attrOrValue, prefix = '', state) => { if (isAttr(attrOrValue)) { return pathTokens(attrOrValue.attr, prefix, state); } else { return valueToken(attrOrValue, prefix, state); } };