dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
134 lines (133 loc) • 6.5 kB
JavaScript
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 { BatchDeleteRequest } from '../../../entity/actions/batchDelete/batchDeleteRequest.js';
import { DynamoDBToolboxError } from '../../../errors/index.js';
import { BatchWriteCommand, execute } from '../../../table/actions/batchWrite/index.js';
import { $entity, QueryCommand } from '../../../table/actions/query/index.js';
import { $sentArgs } from '../../../table/constants.js';
import { interceptable } from '../../../table/decorator.js';
import { $entities, TableAction } from '../../../table/index.js';
import { chunk } from '../../../utils/chunk.js';
import { $options, $query } from './constants.js';
export class DeletePartitionCommand extends TableAction {
constructor(table, entities = [], query, options = {}) {
super(table, entities);
this[$query] = query;
this[$options] = options;
}
entities(...nextEntities) {
return new DeletePartitionCommand(this.table, nextEntities, this[$query], this[$options]);
}
query(nextQuery) {
return new DeletePartitionCommand(this.table, this[$entities], nextQuery, this[$options]);
}
options(nextOptions) {
return new DeletePartitionCommand(this.table, this[$entities], this[$query], typeof nextOptions === 'function' ? nextOptions(this[$options]) : nextOptions);
}
[$sentArgs]() {
if (this[$entities].length === 0) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'DeletePartitionCommand incomplete: Missing "entities" property'
});
}
if (!this[$query]) {
throw new DynamoDBToolboxError('actions.incompleteAction', {
message: 'DeletePartitionCommand incomplete: Missing "query" property'
});
}
return [
this[$entities],
this[$query],
/**
* @debt type "Make any DeletePartitionOptions<...> instance extend base DeletePartitionOptions"
*/
this[$options]
];
}
queryCommand({ exclusiveStartKey } = {}) {
return new QueryCommand(this.table, this[$entities], this[$query], {
...this[$options],
attributes: undefined,
tagEntities: true,
...(exclusiveStartKey !== undefined ? { ExclusiveStartKey: exclusiveStartKey } : {}),
// 1 page <= 1MB and max data of BatchWriteItem is 16MB
maxPages: 16
});
}
params() {
return this.queryCommand().params();
}
async send(documentClientOptions) {
const entitiesByName = {};
this[$entities].forEach(entity => {
entitiesByName[entity.entityName] = entity;
});
const { capacity, tableName } = this[$options];
let lastEvaluatedKey = undefined;
let count = 0;
let scannedCount = 0;
let queryConsumedCapacity = undefined;
let batchWriteConsumedCapacity = [];
let pageIndex = 0;
do {
pageIndex += 1;
const { Items: items = [], Count: pageCount, ScannedCount: pageScannedCount, ConsumedCapacity: pageQueryConsumedCapacity, ...queryOutput } = await this.queryCommand({ exclusiveStartKey: lastEvaluatedKey }).send(documentClientOptions);
for (const itemChunk of chunk(items, 25)) {
if (itemChunk.length === 0) {
continue;
}
const batchDeleteRequests = [];
for (const item of itemChunk) {
const entity = entitiesByName[item[$entity]];
if (entity === undefined) {
continue;
}
batchDeleteRequests.push(new BatchDeleteRequest(entity, item));
}
// TODO: Merge across pages and return ItemCollectionMetrics
const { ConsumedCapacity: chunkBatchWriteConsumedCapacity } = await execute({ maxAttempts: Infinity, capacity, ...documentClientOptions }, this.table
.build(BatchWriteCommand)
.options({ tableName })
.requests(...batchDeleteRequests));
if (chunkBatchWriteConsumedCapacity !== undefined) {
batchWriteConsumedCapacity === null || batchWriteConsumedCapacity === void 0 ? void 0 : batchWriteConsumedCapacity.push(...chunkBatchWriteConsumedCapacity);
}
else {
batchWriteConsumedCapacity = undefined;
}
}
lastEvaluatedKey = queryOutput.LastEvaluatedKey;
if (count !== undefined) {
count = pageCount !== undefined ? count + pageCount : undefined;
}
if (scannedCount !== undefined) {
scannedCount = pageScannedCount !== undefined ? scannedCount + pageScannedCount : undefined;
}
queryConsumedCapacity = pageQueryConsumedCapacity;
} while (lastEvaluatedKey !== undefined);
return {
...(lastEvaluatedKey !== undefined ? { LastEvaluatedKey: lastEvaluatedKey } : {}),
...(count !== undefined ? { Count: count } : {}),
...(scannedCount !== undefined ? { ScannedCount: scannedCount } : {}),
...(batchWriteConsumedCapacity !== undefined
? { BatchWriteConsumedCapacity: batchWriteConsumedCapacity }
: {}),
// return ConsumedCapacity only if one page has been fetched
...(pageIndex === 1
? {
...(queryConsumedCapacity !== undefined
? { QueryConsumedCapacity: queryConsumedCapacity }
: {})
}
: {})
};
}
}
DeletePartitionCommand.actionName = 'deletePartition';
__decorate([
interceptable()
], DeletePartitionCommand.prototype, "send", null);