UNPKG

@moicky/dynamodb

Version:

Contains a collection of convenience functions for working with AWS DynamoDB

113 lines (112 loc) 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteItems = exports.deleteItem = void 0; const client_dynamodb_1 = require("@aws-sdk/client-dynamodb"); const lib_1 = require("../lib"); /** * Deletes an item from the DynamoDB table using its key schema. * @param key - The item with at least the partition key and the sort key (if applicable) of the item to delete. * @param args - The additional arguments to override or specify for {@link DeleteItemCommandInput} * @returns A promise that resolves to the output of {@link DeleteItemCommandOutput} * * @example * Delete a single item in default table * ```javascript * await deleteItem({ * PK: "User/1", * SK: "Book/1", * // fields which are not part of the key schema will be removed * title: "The Great Gatsby", * author: "F. Scott Fitzgerald", * released: 1925, * }); * ``` * @example * Delete a single item in a different table * ```javascript * await deleteItem( * { PK: "User/1", SK: "Book/1" }, * { TableName: "AnotherTable" } * ); * ``` */ async function deleteItem(key, args = {}) { return (0, lib_1.getClient)().send(new client_dynamodb_1.DeleteItemCommand({ Key: (0, lib_1.stripKey)(key, args), ...(0, lib_1.withDefaults)(args, "deleteItem"), TableName: args?.TableName || (0, lib_1.getDefaultTable)(), })); } exports.deleteItem = deleteItem; /** * Deletes unlimited items from the DynamoDB table using their key schema. * @param keys - The items with at least the partition key and the sort key (if applicable) of the items to delete. * @param args - The additional arguments to override or specify for {@link DeleteItemsArgs} * @returns A promise that resolves to void * * @example * Delete items in default table * ```javascript * await deleteItems([ * // fields which are not part of the key schema will be removed * { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 }, * { PK: "User/1", SK: "Book/2" }, * { PK: "User/1", SK: "Book/3" }, * // ... infinite more items (will be grouped into batches of 25 due to aws limit) * // and retried up to 3 times * ]); * ``` * @example * Delete items in a different table * ```javascript * await deleteItems( * [ * // fields which are not part of the key schema will be removed * { PK: "User/1", SK: "Book/1", title: "The Great Gatsby", released: 1925 }, * { PK: "User/1", SK: "Book/2" }, * { PK: "User/1", SK: "Book/3" }, * ], * { TableName: "AnotherTable" } * ); * ``` */ async function deleteItems(keys, args = {}, retry = 0) { args = (0, lib_1.withDefaults)(args, "deleteItems"); const uniqueKeys = Object.values(keys.reduce((acc, key) => { const strippedKey = (0, lib_1.stripKey)(key, args); const keyString = JSON.stringify(strippedKey); if (!acc[keyString]) { acc[keyString] = strippedKey; } return acc; }, {})); return new Promise(async (resolve, reject) => { const batches = (0, lib_1.splitEvery)(uniqueKeys); if (retry > 3) return; const table = args?.TableName || (0, lib_1.getDefaultTable)(); for (const batch of batches) { await (0, lib_1.getClient)() .send(new client_dynamodb_1.BatchWriteItemCommand({ RequestItems: { [table]: batch.map((Key) => ({ DeleteRequest: { Key, }, })), }, ...args, })) .then((res) => { if (res?.UnprocessedItems?.[table]?.length) { if (retry + 1 > 3) return reject(res); return deleteItems(res.UnprocessedItems[table].map((d) => d?.DeleteRequest?.Key), { ...args, TableName: table }, retry + 1); } }) .catch(reject); } resolve(); }); } exports.deleteItems = deleteItems;