UNPKG

dynamodb-toolbox

Version:

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

56 lines (55 loc) 2.65 kB
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; }; import { PutCommand } from '@aws-sdk/lib-dynamodb'; import { EntityFormatter } from '../../../entity/actions/format/index.js'; import { $sentArgs } from '../../../entity/constants.js'; import { interceptable } from '../../../entity/decorator.js'; import { EntityAction } from '../../../entity/index.js'; import { DynamoDBToolboxError } from '../../../errors/index.js'; import { $item, $options } from './constants.js'; import { putItemParams } from './putItemParams/index.js'; export class PutItemCommand extends EntityAction { constructor(entity, item, options = {}) { super(entity); this[$item] = item; this[$options] = options; } item(nextItem) { return new PutItemCommand(this.entity, nextItem, this[$options]); } options(nextOptions) { return new PutItemCommand(this.entity, this[$item], typeof nextOptions === 'function' ? nextOptions(this[$options]) : nextOptions); } [$sentArgs]() { if (!this[$item]) { throw new DynamoDBToolboxError('actions.incompleteAction', { message: 'PutItemCommand incomplete: Missing "item" property' }); } return [this[$item], this[$options]]; } params() { const [item, options] = this[$sentArgs](); return putItemParams(this.entity, item, options); } async send(documentClientOptions) { const { ToolboxItem, ...putItemParams } = this.params(); const commandOutput = await this.entity.table .getDocumentClient() .send(new PutCommand(putItemParams), documentClientOptions); const { Attributes: attributes, ...restCommandOutput } = commandOutput; if (attributes === undefined) { return { ToolboxItem, ...restCommandOutput }; } const formattedItem = new EntityFormatter(this.entity).format(attributes); return { ToolboxItem, Attributes: formattedItem, ...restCommandOutput }; } } PutItemCommand.actionName = 'put'; __decorate([ interceptable() ], PutItemCommand.prototype, "send", null);