Skip to content

Commit f577ff2

Browse files
authored
don't teardown on update (#23)
* don't teardown on update * using batchwrite
1 parent 634ccd6 commit f577ff2

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lib/index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ const AWS = require('aws-sdk');
5757
const s3 = new AWS.S3();
5858
5959
const writeTypeFromAction = (action) => {
60-
if (action === "put")
60+
if (action === "Put")
6161
return "Item";
62-
if (action === "delete")
62+
if (action === "Delete")
6363
return "Key";
6464
}
6565
@@ -79,17 +79,28 @@ const run = async (filename, action) => {
7979
convertEmptyValues: true
8080
});
8181
console.log('sending data to dynamodb');
82-
for(let i = 0; i < seed.length;i++) {
83-
await documentClient[action]({
84-
TableName: '${props.table.tableName}',
85-
[writeTypeFromAction(action)]: seed[i]
82+
do {
83+
const requests = [];
84+
const batch = seed.splice(0, 25);
85+
for (let i = 0; i < batch.length; i++) {
86+
requests.push({
87+
[action + "Request"]: {
88+
[writeTypeFromAction(action)]: batch[i]
89+
}
90+
});
91+
}
92+
await documentClient.batchWrite({
93+
RequestItems: {
94+
'${props.table.tableName}': [...requests]
95+
}
8696
}).promise();
87-
};
97+
}
98+
while (seed.length > 0);
8899
console.log('finished sending data to dynamodb');
89100
}
90101
91102
exports.handler = async (event) => {
92-
if (event.mode === "delete" || event.mode === "update")
103+
if (event.mode === "delete")
93104
await run("teardown.json", "delete");
94105
if (event.mode === "create" || event.mode === "update")
95106
await run("setup.json", "put");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-cdk-dynamodb-seeder",
3-
"version": "1.37.0",
3+
"version": "1.37.1",
44
"description": "A simple CDK JSON seeder for DynamoDB",
55
"scripts": {
66
"build": "jsii",

0 commit comments

Comments
 (0)