UNPKG

dynamodb-toolbox

Version:

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

65 lines (64 loc) 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UpdateTransaction = void 0; const index_js_1 = require("../../../entity/actions/parse/index.js"); const index_js_2 = require("../../../entity/actions/update/expressUpdate/index.js"); const index_js_3 = require("../../../entity/actions/update/updateItemParams/extension/index.js"); const index_js_4 = require("../../../errors/index.js"); const isEmpty_js_1 = require("../../../utils/isEmpty.js"); const omit_js_1 = require("../../../utils/omit.js"); const transaction_js_1 = require("../transactWrite/transaction.js"); const constants_js_1 = require("./constants.js"); const options_js_1 = require("./options.js"); class UpdateTransaction extends transaction_js_1.WriteTransaction { constructor(entity, item, options = {}) { super(entity); this[constants_js_1.$item] = item; this[constants_js_1.$options] = options; } item(nextItem) { return new UpdateTransaction(this.entity, nextItem, this[constants_js_1.$options]); } options(nextOptions) { return new UpdateTransaction(this.entity, this[constants_js_1.$item], typeof nextOptions === 'function' ? nextOptions(this[constants_js_1.$options]) : nextOptions); } params() { var _a; if (!this[constants_js_1.$item]) { throw new index_js_4.DynamoDBToolboxError('actions.incompleteAction', { message: 'UpdateTransaction incomplete: Missing "item" property' }); } const { parsedItem, item, key } = this.entity.build(index_js_1.EntityParser).parse(this[constants_js_1.$item], { mode: 'update', parseExtension: index_js_3.parseUpdateExtension }); const { ExpressionAttributeNames: updateExpressionAttributeNames, ExpressionAttributeValues: updateExpressionAttributeValues, UpdateExpression } = (0, index_js_2.expressUpdate)(this.entity, (0, omit_js_1.omit)(item, ...Object.keys(key))); const options = this[constants_js_1.$options]; const { ExpressionAttributeNames: optionsExpressionAttributeNames, ExpressionAttributeValues: optionsExpressionAttributeValues, ...awsOptions } = (0, options_js_1.parseOptions)(this.entity, options); const ExpressionAttributeNames = { ...optionsExpressionAttributeNames, ...updateExpressionAttributeNames }; const ExpressionAttributeValues = { ...optionsExpressionAttributeValues, ...updateExpressionAttributeValues }; return { /** * @debt type "TODO: Rework extensions & not cast here (use `ParsedItem<ENTITY, { extension: UpdateItemExtension }>`)" */ ToolboxItem: parsedItem, Update: { TableName: (_a = options.tableName) !== null && _a !== void 0 ? _a : this.entity.table.getName(), Key: key, UpdateExpression, ...awsOptions, ...(!(0, isEmpty_js_1.isEmpty)(ExpressionAttributeNames) ? { ExpressionAttributeNames } : {}), ...(!(0, isEmpty_js_1.isEmpty)(ExpressionAttributeValues) ? { ExpressionAttributeValues } : {}) } }; } } exports.UpdateTransaction = UpdateTransaction; UpdateTransaction.actionName = 'transactUpdate';