dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
63 lines (62 loc) • 3.24 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateItemCommand = void 0;
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
const index_js_1 = require("../../../entity/actions/format/index.js");
const constants_js_1 = require("../../../entity/constants.js");
const decorator_js_1 = require("../../../entity/decorator.js");
const index_js_2 = require("../../../entity/index.js");
const index_js_3 = require("../../../errors/index.js");
const constants_js_2 = require("./constants.js");
const index_js_4 = require("./updateItemParams/index.js");
class UpdateItemCommand extends index_js_2.EntityAction {
constructor(entity, item, options = {}) {
super(entity);
this[constants_js_2.$item] = item;
this[constants_js_2.$options] = options;
}
item(nextItem) {
return new UpdateItemCommand(this.entity, nextItem, this[constants_js_2.$options]);
}
options(nextOptions) {
return new UpdateItemCommand(this.entity, this[constants_js_2.$item], typeof nextOptions === 'function' ? nextOptions(this[constants_js_2.$options]) : nextOptions);
}
[constants_js_1.$sentArgs]() {
if (!this[constants_js_2.$item]) {
throw new index_js_3.DynamoDBToolboxError('actions.incompleteAction', {
message: 'UpdateItemCommand incomplete: Missing "item" property'
});
}
return [this[constants_js_2.$item], this[constants_js_2.$options]];
}
params() {
const [item, options] = this[constants_js_1.$sentArgs]();
return (0, index_js_4.updateItemParams)(this.entity, item, options);
}
async send(documentClientOptions) {
const { ToolboxItem, ...getItemParams } = this.params();
const commandOutput = await this.entity.table
.getDocumentClient()
.send(new lib_dynamodb_1.UpdateCommand(getItemParams), documentClientOptions);
const { Attributes: attributes, ...restCommandOutput } = commandOutput;
if (attributes === undefined) {
return { ToolboxItem, ...restCommandOutput };
}
const { returnValues } = this[constants_js_2.$options];
const formattedItem = new index_js_1.EntityFormatter(this.entity).format(attributes, {
partial: returnValues === 'UPDATED_NEW' || returnValues === 'UPDATED_OLD'
});
return { ToolboxItem, Attributes: formattedItem, ...restCommandOutput };
}
}
exports.UpdateItemCommand = UpdateItemCommand;
UpdateItemCommand.actionName = 'updateItem';
__decorate([
(0, decorator_js_1.interceptable)()
], UpdateItemCommand.prototype, "send", null);