UNPKG

dynamodb-toolbox

Version:

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

45 lines (44 loc) 1.92 kB
import { DynamoDBToolboxError } from '../../../errors/index.js'; import { rejectExtraOptions } from '../../../options/rejectExtraOptions.js'; import { parseTableNameOption } from '../../../options/tableName.js'; import { $entities, TableAction } from '../../../table/index.js'; import { $options, $requests } from './constants.js'; export class BatchWriteCommand extends TableAction { constructor(table, entities = [], requests, options = {}) { super(table, entities); this[$requests] = requests; this[$options] = options; } requests(...requests) { const entities = []; const entityNames = new Set(); for (const request of requests) { if (entityNames.has(request.entity.entityName)) { continue; } entities.push(request.entity); entityNames.add(request.entity.entityName); } return new BatchWriteCommand(this.table, entities, requests); } options(nextOptions) { return new BatchWriteCommand(this.table, this[$entities], this[$requests], typeof nextOptions === 'function' ? nextOptions(this[$options]) : nextOptions); } params() { var _a; if (this[$requests] === undefined || this[$requests].length === 0) { throw new DynamoDBToolboxError('actions.incompleteAction', { message: 'BatchWriteCommand incomplete: No BatchWriteRequest supplied' }); } const { tableName, ...extraOptions } = (_a = this[$options]) !== null && _a !== void 0 ? _a : {}; rejectExtraOptions(extraOptions); if (tableName) { parseTableNameOption(tableName); } return { [tableName !== null && tableName !== void 0 ? tableName : this.table.getName()]: this[$requests].map(request => request.params()) }; } } BatchWriteCommand.actionName = 'batchWrite';