Skip to content

Commit

Permalink
feat: add dynamodb bulk delete operations
Browse files Browse the repository at this point in the history
  • Loading branch information
theBenForce committed Mar 6, 2020
1 parent a29cf32 commit c00ec02
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/data-migration/src/DriverTypes/NoSQL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import Driver from "./Driver";
import { Observable } from "rxjs";

export default interface NoSqlDriver<P, R> extends Driver<P, R> {
getAllRecords: <T>() => Observable<T>;
putRecord: <T>(record: T) => Promise<T>;
putRecordsBulk: <T>(records: Array<T>) => Promise<T>;
getAllRecords<T>(): Observable<T>;
putRecord<T>(record: T): Promise<T>;
deleteRecord<T>(key: T): Promise<any>;
putRecordsBulk<T>(records: Array<T>): Promise<T>;
deleteRecordsBulk<T>(keys: Array<T>): Promise<any>;
}
33 changes: 33 additions & 0 deletions plugins/drivers/dynamodb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ const dynamoDbDriver: DriverBuilder<DynamoDbParameters, DynamoDbDriver> = (

return records;
},

async deleteRecord<T>(key: T) {
const result = await DocumentDb.delete({
TableName,
Key: key,
}).promise();

return result;
},

async deleteRecordsBulk<T>(keys: Array<T>) {
const writeItems = keys.map(
(Key) =>
({
DeleteRequest: { Key },
} as AWS.DynamoDB.DocumentClient.WriteRequest)
);

for (let index = 0; index < writeItems.length; index += 25) {
const deleteRequest = {
RequestItems: {},
} as AWS.DynamoDB.DocumentClient.BatchWriteItemInput;

deleteRequest.RequestItems[TableName] = writeItems.slice(
index,
Math.min(index + 25, writeItems.length)
);

logger(`Deleting with request ${JSON.stringify(deleteRequest)}`);

await DocumentDb.batchWrite(deleteRequest).promise();
}
},
} as DynamoDbDriver;
};

Expand Down

0 comments on commit c00ec02

Please sign in to comment.