dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
49 lines (48 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BatchWriteCommand = void 0;
const index_js_1 = require("../../../errors/index.js");
const rejectExtraOptions_js_1 = require("../../../options/rejectExtraOptions.js");
const tableName_js_1 = require("../../../options/tableName.js");
const index_js_2 = require("../../../table/index.js");
const constants_js_1 = require("./constants.js");
class BatchWriteCommand extends index_js_2.TableAction {
constructor(table, entities = [], requests, options = {}) {
super(table, entities);
this[constants_js_1.$requests] = requests;
this[constants_js_1.$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[index_js_2.$entities], this[constants_js_1.$requests], typeof nextOptions === 'function' ? nextOptions(this[constants_js_1.$options]) : nextOptions);
}
params() {
var _a;
if (this[constants_js_1.$requests] === undefined || this[constants_js_1.$requests].length === 0) {
throw new index_js_1.DynamoDBToolboxError('actions.incompleteAction', {
message: 'BatchWriteCommand incomplete: No BatchWriteRequest supplied'
});
}
const { tableName, ...extraOptions } = (_a = this[constants_js_1.$options]) !== null && _a !== void 0 ? _a : {};
(0, rejectExtraOptions_js_1.rejectExtraOptions)(extraOptions);
if (tableName) {
(0, tableName_js_1.parseTableNameOption)(tableName);
}
return {
[tableName !== null && tableName !== void 0 ? tableName : this.table.getName()]: this[constants_js_1.$requests].map(request => request.params())
};
}
}
exports.BatchWriteCommand = BatchWriteCommand;
BatchWriteCommand.actionName = 'batchWrite';