From 8f96f40f1aa628859aaf25ce3005c5b45a978a65 Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Tue, 23 Jan 2024 17:06:23 +0000 Subject: [PATCH] feat: seed using batchwrite --- seed.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/seed.js b/seed.js index 26e7374..2805ca7 100644 --- a/seed.js +++ b/seed.js @@ -4,24 +4,32 @@ const { REGION, TABLE_NAME } = require('./constants'); const dynamoDb = new AWS.DynamoDB.DocumentClient({ region: REGION }); const movies = require('./tmdb_movies.json'); -async function uploadMovie(movie) { +async function uploadMoviesBatch(moviesBatch) { const params = { - TableName: TABLE_NAME, - Item: movie, + RequestItems: { + [TABLE_NAME]: moviesBatch.map(movie => ({ + PutRequest: { + Item: movie, + }, + })), + }, }; try { - await dynamoDb.put(params).promise(); - console.log(`Movie added: ${movie.title}`); + await dynamoDb.batchWrite(params).promise(); + const movieTitles = moviesBatch.map(movie => movie.title); + console.log(`Movies batch added:`, movieTitles); } catch (error) { - console.error(`Error adding movie: ${movie.title}`, error); + console.error('Error adding movies batch', error); } } async function uploadAllMovies() { - for (const movie of movies) { - await uploadMovie(movie); - await new Promise(resolve => setTimeout(resolve, 100)); + const BATCH_SIZE = 25; + for (let i = 0; i < movies.length; i += BATCH_SIZE) { + const moviesBatch = movies.slice(i, i + BATCH_SIZE); + await uploadMoviesBatch(moviesBatch); + await new Promise(resolve => setTimeout(resolve, 100)); // Throttling } }