dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
60 lines (59 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCommandInput = exports.execute = exports.formatResponses = void 0;
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
const index_js_1 = require("../../../entity/actions/format/index.js");
const index_js_2 = require("../../../errors/index.js");
const capacity_js_1 = require("../../../options/capacity.js");
const rejectExtraOptions_js_1 = require("../../../options/rejectExtraOptions.js");
const constants_js_1 = require("./getTransaction/constants.js");
const getTransaction_js_1 = require("./getTransaction/getTransaction.js");
const formatResponses = (responses, ...transactions) => responses.map(({ Item: item }, index) => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const transaction = transactions[index];
const transactionEntity = transaction.entity;
const { attributes } = transaction[constants_js_1.$options];
return {
Item: item
? new index_js_1.EntityFormatter(transactionEntity).format(item, attributes ? { attributes } : {})
: undefined
};
});
exports.formatResponses = formatResponses;
const execute = async (..._transactions) => {
const [headTransactionOrOptions = {}, ...tailTransactions] = _transactions;
const transactions = tailTransactions;
let options = {};
if (headTransactionOrOptions instanceof getTransaction_js_1.GetTransaction) {
transactions.unshift(headTransactionOrOptions);
}
else {
options = headTransactionOrOptions;
}
const firstTransaction = transactions[0];
if (firstTransaction === undefined) {
throw new index_js_2.DynamoDBToolboxError('actions.incompleteAction', {
message: 'transactGet incomplete: No GetTransaction supplied'
});
}
const { documentClient, capacity, ...documentClientOptions } = options;
const docClient = documentClient !== null && documentClient !== void 0 ? documentClient : firstTransaction.entity.table.getDocumentClient();
const { Responses, ...restResponse } = await docClient.send(new lib_dynamodb_1.TransactGetCommand((0, exports.getCommandInput)(transactions, { capacity })), documentClientOptions);
if (Responses === undefined) {
return restResponse;
}
return {
...restResponse,
Responses: (0, exports.formatResponses)(Responses, ...transactions)
};
};
exports.execute = execute;
const getCommandInput = (transactions, options = {}) => {
const { capacity, ...extraOptions } = options;
(0, rejectExtraOptions_js_1.rejectExtraOptions)(extraOptions);
return {
TransactItems: transactions.map(command => command.params()),
...(capacity !== undefined ? { ReturnConsumedCapacity: (0, capacity_js_1.parseCapacityOption)(capacity) } : {})
};
};
exports.getCommandInput = getCommandInput;