From 16d324bd69ea5340e5a0a68efe492d4122b55ee9 Mon Sep 17 00:00:00 2001 From: Cameron Date: Fri, 24 Dec 2021 21:36:00 +0200 Subject: [PATCH 1/2] Added refactor for batchWrite --- README.md | 80 ++++++++++++++++++++++++++----------------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index a949c11..0210a2d 100644 --- a/README.md +++ b/README.md @@ -353,53 +353,49 @@ const seedData = fetchedData.map((item) => { } }) -/* We can only batch-write 25 items at a time, - so we'll store both the quotient, as well as what's left. - */ + +function* executeEach(array: Array, sectionSize: number): Generator> { -let quotient = Math.floor(seedData.length / 25) -const remainder = (seedData.length % 25) + let batchMultiplier = 1; -/* Upload in increments of 25 */ + // split the items into batches + for (let i = 0; i < array.length; i += sectionSize) { -let batchMultiplier = 1 -while (quotient > 0) { - for (let i = 0; i < seedData.length - 1; i += 25) { - await docClient.batchWrite( - { - RequestItems: { - YOUR_TABLE_NAME: seedData.slice(i, 25 * batchMultiplier), - }, - }, - (err, data) => { - if (err) { - console.log('something went wrong...') - } else { - console.log('yay...uploaded!') - } - } - ).promise() - console.log({ quotient }) - ++batchMultiplier - --quotient + const subArr = array.slice(i, sectionSize * batchMultiplier); + + batchMultiplier++; + + yield subArr; } + } -/* Upload the remaining items (less than 25) */] -if(remainder > 0){ - await docClient.batchWrite( - { - RequestItems: { - YOUR_TABLE_NAME: seedData.slice(seedData.length - remainder), - }, - }, - (err, data) => { - if (err) { - console.log('something went wrong...') - } else { - console.log('yay...the remainder uploaded!') - } +/* Upload in increments of 25 */ + const batchedItems = executeEach>(seedData, 25); + + while (true) { + + const currentItems = batchedItems.next(); + + if (currentItems.done) break; + + try { + + const result = await docClient.batchWrite( + { + RequestItems: { + tableName: currentItems + }, + } + ).promise() + + console.log(result); + + } catch (err) { + console.log("Error!"); + console.log(err); } - ).promise() -} + + + } ``` From b6e7568d950408b054cb80c9983173313e2f60b5 Mon Sep 17 00:00:00 2001 From: ilikepi63 Date: Fri, 24 Dec 2021 21:38:18 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0210a2d..9d3cdc1 100644 --- a/README.md +++ b/README.md @@ -384,7 +384,7 @@ function* executeEach(array: Array, sectionSize: number): Generator