Skip to content

Commit

Permalink
fix: add batching to clearAllItems (#629)
Browse files Browse the repository at this point in the history
* fix: add batching to clearAllItems

* fix: add types to chunk helper
  • Loading branch information
Ankcorn authored Jun 22, 2022
1 parent dc2c8b8 commit 54b37b7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
46 changes: 46 additions & 0 deletions src/utils/dynamoDb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,52 @@ describe('dynamoDb utils', () => {
},
});
});
test('should call batchWrite multiple times for db containing more than 25 items', async () => {
const describeTable = db().describeTable;
const describeTablePromise = describeTable().promise;
const table = { KeySchema: [{ AttributeName: 'id' }] };
describeTablePromise.mockReturnValue(Promise.resolve({ Table: table }));

const scan = documentClient().scan;
const batchWrite = documentClient().batchWrite;
const scanPromise = scan().promise;
const items = Array(30)
.fill({ id: 'id' })
.map((el, i) => ({
id: el.id + i,
}));
scanPromise.mockReturnValue(Promise.resolve({ Items: items }));

jest.clearAllMocks();

await clearAllItems(region, tableName);

expect(scan).toHaveBeenCalledTimes(1);
expect(scan).toHaveBeenCalledWith({
AttributesToGet: table.KeySchema.map((k) => k.AttributeName),
TableName: tableName,
});

expect(batchWrite).toHaveBeenCalledTimes(2);
const batch1 = items.splice(0, 25);
const batch2 = items;
expect(batch1.length).toBe(25);
expect(batch2.length).toBe(5);
expect(batchWrite).toHaveBeenCalledWith({
RequestItems: {
[tableName]: batch1.map((item) => ({
DeleteRequest: { Key: { id: item.id } },
})),
},
});
expect(batchWrite).toHaveBeenCalledWith({
RequestItems: {
[tableName]: batch2.map((item) => ({
DeleteRequest: { Key: { id: item.id } },
})),
},
});
});
});

describe('writeItems', () => {
Expand Down
20 changes: 14 additions & 6 deletions src/utils/dynamoDb.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import AWS = require('aws-sdk');

function* chunks<T>(arr: T[], n: number): Generator<T[], void> {
for (let i = 0; i < arr.length; i += n) {
yield arr.slice(i, i + n);
}
}

const itemToKey = (
item: AWS.DynamoDB.DocumentClient.AttributeMap,
keySchema: AWS.DynamoDB.KeySchemaElement[],
Expand Down Expand Up @@ -31,13 +37,15 @@ export const clearAllItems = async (region: string, tableName: string) => {
const items = scanResult.Items || [];

if (items.length > 0) {
const deleteRequests = items.map((item) => ({
DeleteRequest: { Key: itemToKey(item, keySchema) },
}));
for (const chunk of chunks(items, 25)) {
const deleteRequests = chunk.map((item) => ({
DeleteRequest: { Key: itemToKey(item, keySchema) },
}));

await db
.batchWrite({ RequestItems: { [tableName]: deleteRequests } })
.promise();
await db
.batchWrite({ RequestItems: { [tableName]: deleteRequests } })
.promise();
}
}
};

Expand Down

0 comments on commit 54b37b7

Please sign in to comment.