Skip to content

Commit

Permalink
feat: seed using batchwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed Jan 23, 2024
1 parent db50a28 commit 8f96f40
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit 8f96f40

Please sign in to comment.