Created
March 25, 2019 00:46
-
-
Save andreigec/fe75eb5e968df60af72feee006d663b7 to your computer and use it in GitHub Desktop.
AWS dynamodb batch write helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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