UNPKG

dynamodb-toolbox

Version:

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

87 lines (86 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCommandInput = exports.execute = void 0; const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb"); const index_js_1 = require("../../../errors/index.js"); const capacity_js_1 = require("../../../options/capacity.js"); const metrics_js_1 = require("../../../options/metrics.js"); const isEmpty_js_1 = require("../../../utils/isEmpty.js"); const batchWriteCommand_js_1 = require("./batchWriteCommand.js"); const execute = async (..._commands) => { const [headCommandOrOptions = {}, ...tailCommands] = _commands; const commands = tailCommands; let options = {}; if (headCommandOrOptions instanceof batchWriteCommand_js_1.BatchWriteCommand) { commands.unshift(headCommandOrOptions); } else { options = headCommandOrOptions; } const firstCommand = commands[0]; if (firstCommand === undefined) { throw new index_js_1.DynamoDBToolboxError('actions.incompleteAction', { message: 'Cannot execute BatchWriteCommands: No BatchWriteCommand supplied' }); } const { maxAttempts = 1, metrics, capacity, documentClient, ...documentClientOptions } = options; const docClient = documentClient !== null && documentClient !== void 0 ? documentClient : firstCommand.table.getDocumentClient(); const { RequestItems: initialRequestItems, ...commandOptions } = (0, exports.getCommandInput)(commands, { metrics, capacity }); let attemptCount = 0; let requestItems = initialRequestItems; let unprocessedItems = {}; let consumedCapacity = undefined; let collectionMetrics = undefined; let responseMetadata = {}; do { attemptCount += 1; const { UnprocessedItems: attemptUnprocessedItems = {}, ConsumedCapacity: attemptConsumedCapacity, ItemCollectionMetrics: attemptCollectionMetrics, $metadata: attemptMetadata } = await docClient.send(new lib_dynamodb_1.BatchWriteCommand({ RequestItems: requestItems, ...commandOptions }), documentClientOptions); requestItems = attemptUnprocessedItems; unprocessedItems = attemptUnprocessedItems; consumedCapacity = attemptConsumedCapacity; collectionMetrics = attemptCollectionMetrics; responseMetadata = attemptMetadata; } while (attemptCount < maxAttempts && !(0, isEmpty_js_1.isEmpty)(unprocessedItems)); return { ...(unprocessedItems !== undefined ? { UnprocessedItems: unprocessedItems } : {}), $metadata: {}, // return ConsumedCapacity, ItemCollectionMetrics & $metadata only if one attempt has been tried ...(attemptCount === 1 ? { ...(consumedCapacity !== undefined ? { ConsumedCapacity: consumedCapacity } : {}), ...(collectionMetrics !== undefined ? { ItemCollectionMetrics: collectionMetrics } : {}), ...(responseMetadata !== undefined ? { $metadata: responseMetadata } : {}) } : {}) }; }; exports.execute = execute; const getCommandInput = (commands, options = {}) => { const requestItems = {}; if (commands.length === 0) { throw new index_js_1.DynamoDBToolboxError('actions.incompleteAction', { message: 'batchWrite arguments incomplete: No BatchWriteCommand supplied' }); } for (const command of commands) { const commandParams = command.params(); for (const tableName of Object.keys(commandParams)) { if (tableName in requestItems) { throw new index_js_1.DynamoDBToolboxError('actions.invalidAction', { message: `Two BatchWriteCommands detected for table: ${tableName}. Please provide only one BatchWriteCommand per table` }); } } Object.assign(requestItems, commandParams); } const { capacity, metrics } = options; return { RequestItems: requestItems, ...(capacity !== undefined ? { ReturnConsumedCapacity: (0, capacity_js_1.parseCapacityOption)(capacity) } : {}), ...(metrics !== undefined ? { ReturnItemCollectionMetrics: (0, metrics_js_1.parseMetricsOption)(metrics) } : {}) }; }; exports.getCommandInput = getCommandInput;