Skip to content

Added refactor for batchWrite #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(array: Array<T>, sectionSize: number): Generator<Array<T>> {

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<Record<string, string>>(seedData, 25);

while (true) {

const currentItems = batchedItems.next();

if (currentItems.done) break;

try {

const result = await docClient.batchWrite(
{
RequestItems: {
YOUR_TABLE_NAME: currentItems
},
}
).promise()

console.log(result);

} catch (err) {
console.log("Error!");
console.log(err);
}
).promise()
}


}
```