Skip to content

Instantly share code, notes, and snippets.

@andreigec
Created March 25, 2019 00:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreigec/fe75eb5e968df60af72feee006d663b7 to your computer and use it in GitHub Desktop.
Save andreigec/fe75eb5e968df60af72feee006d663b7 to your computer and use it in GitHub Desktop.
AWS dynamodb batch write helper
function breakArrayIntoGroups(data, maxPerGroup) {
const groups = [];
for (let index = 0; index < data.length; index += maxPerGroup) {
groups.push(data.slice(index, index + maxPerGroup));
}
return groups;
}
const dyn = (items, table) =>
new Promise(async res => {
try {
const datagr = breakArrayIntoGroups(items, 25);
for (let i = 0; i < datagr.length; i += 1) {
const po = [];
for (let i2 = 0; i2 < datagr[i].length; i2 += 1) {
const item = datagr[i][i2];
po.push({ PutRequest: { Item: item } });
}
const params = { RequestItems: { [`${table}`]: po } };
const db = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
try {
// eslint-disable-next-line no-await-in-loop
await db.batchWriteItem(params).promise();
} catch (e) {
console.error(`failed with:${po.length} on:${e}`);
}
}
} catch (e) {
console.error(`failed:${e}`);
}
res();
});
//eg
var items = [{ id: 1 }];
await dyn(items, "testtable");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment