UNPKG

dynamodb-toolbox

Version:

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

138 lines (137 loc) 7.06 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeletePartitionCommand = void 0; const batchDeleteRequest_js_1 = require("../../../entity/actions/batchDelete/batchDeleteRequest.js"); const index_js_1 = require("../../../errors/index.js"); const index_js_2 = require("../../../table/actions/batchWrite/index.js"); const index_js_3 = require("../../../table/actions/query/index.js"); const constants_js_1 = require("../../../table/constants.js"); const decorator_js_1 = require("../../../table/decorator.js"); const index_js_4 = require("../../../table/index.js"); const chunk_js_1 = require("../../../utils/chunk.js"); const constants_js_2 = require("./constants.js"); class DeletePartitionCommand extends index_js_4.TableAction { constructor(table, entities = [], query, options = {}) { super(table, entities); this[constants_js_2.$query] = query; this[constants_js_2.$options] = options; } entities(...nextEntities) { return new DeletePartitionCommand(this.table, nextEntities, this[constants_js_2.$query], this[constants_js_2.$options]); } query(nextQuery) { return new DeletePartitionCommand(this.table, this[index_js_4.$entities], nextQuery, this[constants_js_2.$options]); } options(nextOptions) { return new DeletePartitionCommand(this.table, this[index_js_4.$entities], this[constants_js_2.$query], typeof nextOptions === 'function' ? nextOptions(this[constants_js_2.$options]) : nextOptions); } [constants_js_1.$sentArgs]() { if (this[index_js_4.$entities].length === 0) { throw new index_js_1.DynamoDBToolboxError('actions.incompleteAction', { message: 'DeletePartitionCommand incomplete: Missing "entities" property' }); } if (!this[constants_js_2.$query]) { throw new index_js_1.DynamoDBToolboxError('actions.incompleteAction', { message: 'DeletePartitionCommand incomplete: Missing "query" property' }); } return [ this[index_js_4.$entities], this[constants_js_2.$query], /** * @debt type "Make any DeletePartitionOptions<...> instance extend base DeletePartitionOptions" */ this[constants_js_2.$options] ]; } queryCommand({ exclusiveStartKey } = {}) { return new index_js_3.QueryCommand(this.table, this[index_js_4.$entities], this[constants_js_2.$query], { ...this[constants_js_2.$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[index_js_4.$entities].forEach(entity => { entitiesByName[entity.entityName] = entity; }); const { capacity, tableName } = this[constants_js_2.$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 (0, chunk_js_1.chunk)(items, 25)) { if (itemChunk.length === 0) { continue; } const batchDeleteRequests = []; for (const item of itemChunk) { const entity = entitiesByName[item[index_js_3.$entity]]; if (entity === undefined) { continue; } batchDeleteRequests.push(new batchDeleteRequest_js_1.BatchDeleteRequest(entity, item)); } // TODO: Merge across pages and return ItemCollectionMetrics const { ConsumedCapacity: chunkBatchWriteConsumedCapacity } = await (0, index_js_2.execute)({ maxAttempts: Infinity, capacity, ...documentClientOptions }, this.table .build(index_js_2.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 } : {}) } : {}) }; } } exports.DeletePartitionCommand = DeletePartitionCommand; DeletePartitionCommand.actionName = 'deletePartition'; __decorate([ (0, decorator_js_1.interceptable)() ], DeletePartitionCommand.prototype, "send", null);