UNPKG

@nova-odm/expressions

Version:

Composable expression objects for Amazon DynamoDB

118 lines 4.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateExpression = void 0; var tslib_1 = require("tslib"); var AttributePath_1 = require("./AttributePath"); var FunctionExpression_1 = require("./FunctionExpression"); var MathematicalExpression_1 = require("./MathematicalExpression"); /** * An object representing a DynamoDB update expression. */ var UpdateExpression = /** @class */ (function () { function UpdateExpression() { this.toAdd = new Map(); this.toDelete = new Map(); this.toRemove = new Set(); this.toSet = new Map(); } /** * Add a directive to the expression's `add` clause. */ UpdateExpression.prototype.add = function (path, value) { this.toAdd.set(AttributePath_1.AttributePath.isAttributePath(path) ? path : new AttributePath_1.AttributePath(path), value); }; /** * Add a directive to the expression's `delete` clause. */ UpdateExpression.prototype.delete = function (path, value) { this.toDelete.set(AttributePath_1.AttributePath.isAttributePath(path) ? path : new AttributePath_1.AttributePath(path), value); }; /** * Add a directive to the expression's `remove` clause. */ UpdateExpression.prototype.remove = function (path) { this.toRemove.add(AttributePath_1.AttributePath.isAttributePath(path) ? path : new AttributePath_1.AttributePath(path)); }; /** * Add a directive to the expression's `set` clause. */ UpdateExpression.prototype.set = function (path, value) { this.toSet.set(AttributePath_1.AttributePath.isAttributePath(path) ? path : new AttributePath_1.AttributePath(path), value); }; UpdateExpression.prototype.serialize = function (attributes) { var e_1, _a, e_2, _b, e_3, _c, e_4, _d; var clauses = []; var phrases = []; try { for (var _e = tslib_1.__values([ [this.toAdd, 'ADD'], [this.toDelete, 'DELETE'], ]), _f = _e.next(); !_f.done; _f = _e.next()) { var _g = tslib_1.__read(_f.value, 2), mapping = _g[0], verb = _g[1]; try { for (var _h = (e_2 = void 0, tslib_1.__values(mapping.entries())), _j = _h.next(); !_j.done; _j = _h.next()) { var _k = tslib_1.__read(_j.value, 2), key = _k[0], value = _k[1]; phrases.push("".concat(attributes.addName(key), " ").concat(attributes.addValue(value))); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_j && !_j.done && (_b = _h.return)) _b.call(_h); } finally { if (e_2) throw e_2.error; } } if (phrases.length > 0) { clauses.push("".concat(verb, " ").concat(phrases.join(', '))); phrases.length = 0; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_f && !_f.done && (_a = _e.return)) _a.call(_e); } finally { if (e_1) throw e_1.error; } } try { for (var _l = tslib_1.__values(this.toSet.entries()), _m = _l.next(); !_m.done; _m = _l.next()) { var _o = tslib_1.__read(_m.value, 2), key = _o[0], value = _o[1]; phrases.push("".concat(attributes.addName(key), " = ").concat(FunctionExpression_1.FunctionExpression.isFunctionExpression(value) || MathematicalExpression_1.MathematicalExpression.isMathematicalExpression(value) ? value.serialize(attributes) : attributes.addValue(value))); } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (_m && !_m.done && (_c = _l.return)) _c.call(_l); } finally { if (e_3) throw e_3.error; } } if (phrases.length > 0) { clauses.push("SET ".concat(phrases.join(', '))); phrases.length = 0; } try { for (var _p = tslib_1.__values(this.toRemove), _q = _p.next(); !_q.done; _q = _p.next()) { var keyToRemove = _q.value; phrases.push(attributes.addName(keyToRemove)); } } catch (e_4_1) { e_4 = { error: e_4_1 }; } finally { try { if (_q && !_q.done && (_d = _p.return)) _d.call(_p); } finally { if (e_4) throw e_4.error; } } if (phrases.length > 0) { clauses.push("REMOVE ".concat(phrases.join(', '))); phrases.length = 0; } return clauses.join(' '); }; return UpdateExpression; }()); exports.UpdateExpression = UpdateExpression; //# sourceMappingURL=UpdateExpression.js.map