UNPKG

@nova-odm/expressions

Version:

Composable expression objects for Amazon DynamoDB

76 lines 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExpressionAttributes = void 0; var tslib_1 = require("tslib"); var AttributePath_1 = require("./AttributePath"); var auto_marshaller_1 = require("@nova-odm/auto-marshaller"); var AttributeValue_1 = require("./AttributeValue"); /** * An object that manages expression attribute name and value substitution. */ var ExpressionAttributes = /** @class */ (function () { function ExpressionAttributes() { this.names = {}; this.values = {}; this.marshaller = new auto_marshaller_1.Marshaller(); this.nameMap = {}; this._ctr = 0; } /** * Add an attribute path to this substitution context. * * @returns The substitution value to use in the expression. The same * attribute name will always be converted to the same substitution value * when supplied to the same ExpressionAttributes object multiple times. */ ExpressionAttributes.prototype.addName = function (path) { var e_1, _a; if (AttributePath_1.AttributePath.isAttributePath(path)) { var escapedPath = ''; try { for (var _b = tslib_1.__values(path.elements), _c = _b.next(); !_c.done; _c = _b.next()) { var element = _c.value; if (element.type === 'AttributeName') { escapedPath += ".".concat(this.addAttributeName(element.name)); } else { escapedPath += "[".concat(element.index, "]"); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return escapedPath.substring(1); } return this.addName(new AttributePath_1.AttributePath(path)); }; /** * Add an attribute value to this substitution context. * * @returns The substitution value to use in the expression. */ ExpressionAttributes.prototype.addValue = function (value) { var modeledAttrValue = AttributeValue_1.AttributeValue.isAttributeValue(value) ? value.marshalled : this.marshaller.marshallValue(value); var substitution = ":val".concat(this._ctr++); // @ts-ignore this.values[substitution] = modeledAttrValue; return substitution; }; ExpressionAttributes.prototype.addAttributeName = function (attributeName) { if (!(attributeName in this.nameMap)) { this.nameMap[attributeName] = "#attr".concat(this._ctr++); this.names[this.nameMap[attributeName]] = attributeName; } return this.nameMap[attributeName]; }; return ExpressionAttributes; }()); exports.ExpressionAttributes = ExpressionAttributes; //# sourceMappingURL=ExpressionAttributes.js.map