UNPKG

dynamodb-toolbox

Version:

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

58 lines (57 loc) 2.62 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 { DeleteCommand } 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 { $key, $options } from './constants.js'; import { deleteItemParams } from './deleteItemParams/index.js'; export class DeleteItemCommand extends EntityAction { constructor(entity, key, options = {}) { super(entity); this[$key] = key; this[$options] = options; } key(nextKey) { return new DeleteItemCommand(this.entity, nextKey, this[$options]); } options(nextOptions) { return new DeleteItemCommand(this.entity, this[$key], typeof nextOptions === 'function' ? nextOptions(this[$options]) : nextOptions); } [$sentArgs]() { if (!this[$key]) { throw new DynamoDBToolboxError('actions.incompleteAction', { message: 'DeleteItemCommand incomplete: Missing "key" property' }); } return [this[$key], this[$options]]; } params() { return deleteItemParams(this.entity, ...this[$sentArgs]()); } async send(documentClientOptions) { const deleteItemParams = this.params(); const commandOutput = await this.entity.table .getDocumentClient() .send(new DeleteCommand(deleteItemParams), documentClientOptions); const { Attributes: attributes, ...restCommandOutput } = commandOutput; if (attributes === undefined) { return restCommandOutput; } const formattedItem = new EntityFormatter(this.entity).format(attributes); return { Attributes: formattedItem, ...restCommandOutput }; } } DeleteItemCommand.actionName = 'delete'; __decorate([ interceptable() ], DeleteItemCommand.prototype, "send", null);