DEV Community

Cover image for Populate DynamoDB with BatchWriteItem & TypeScript
Radzion Chachura
Radzion Chachura

Posted on • Originally published at radzion.com

Populate DynamoDB with BatchWriteItem & TypeScript

Watch on YouTube

Let's populate a DynamoDB table using BatchWriteItem.

I have a list of coupon codes for my product, and I want to move them into a DynamoDB table.

import uuid from "uuid/v4"

import { tableName } from "../src/shared/db/tableName"
import { documentClient } from "../src/shared/db"

const batchWriteItemMax = 25

const appSumoCodesNumber = 10000

const populateAppSumoCodes = async () => {
  const codes = Array.from({ length: appSumoCodesNumber }, () => uuid())
  const items = codes.map((id) => ({ id }))

  const itemsBatchesNumber = Math.ceil(items.length / batchWriteItemMax)
  await Promise.all(
    Array.from({ length: itemsBatchesNumber }, (_, i) => i).map(
      async (batchIndex) => {
        const batchItems = items.slice(
          batchWriteItemMax * batchIndex,
          batchWriteItemMax * (batchIndex + 1)
        )

        await documentClient
          .batchWrite({
            RequestItems: {
              [tableName.appSumoCodes]: batchItems.map((Item) => ({
                PutRequest: {
                  Item,
                },
              })),
            },
          })
          .promise()
      }
    )
  )
}

populateAppSumoCodes()
Enter fullscreen mode Exit fullscreen mode

First, I convert them into a list of items.

We can't just upload all of them at once. DynamoDB allows inserting a maximum of 25 items at once.

We need to calculate how many batches we should have. For example, if there are 60 items, we'll insert 3 batches.

After that, we create an array with the length of itemsBatchesNumber, and use Promise.all to iterate over it.

To populate the table, we slice a batch from a list of items and pass it to the batchWrite method of DynamoDB documentClient.

Now let's set environment variables and run the migration with ts-node.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay