DEV Community

Cover image for Get All Items From DynamoDB Table with TypeScript
Radzion Chachura
Radzion Chachura

Posted on • Originally published at radzion.com

Get All Items From DynamoDB Table with TypeScript

Watch on YouTube

I need to get a CSV file with all the coupon codes stored in a DynamoDB table.

import { Key } from "aws-sdk/clients/dynamodb"
import fs from "fs"

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

const getAllAppSumoCodeIds = async () => {
  const ids: string[] = []

  const recursiveProcess = async (lastEvaluatedKey?: Key) => {
    const { Items = [], LastEvaluatedKey } = await documentClient
      .scan({
        TableName: tableName.appSumoCodes,
        ExclusiveStartKey: lastEvaluatedKey,
        ExpressionAttributeNames: {
          "#id": "id",
        },
        ProjectionExpression: "#id",
      })
      .promise()

    ids.push(...Items.map((item) => item.id))

    if (LastEvaluatedKey) {
      await recursiveProcess(LastEvaluatedKey)
    }
  }

  await recursiveProcess()

  return ids
}
Enter fullscreen mode Exit fullscreen mode

We will use the scan method to get all the items from the table. But we can't get everything in one go. The method returns a paginated result.

We will use recursion instead of a loop to run the scan method until it doesn't return LastEvaluatedKey.

We only need to pass the table name and last evaluated key to the scan method.

But since I only need an id, I'll also set ExpressionAttributeNames and ProjectionExpression.

Now we can set AWS environment variables and run the file with ts-node.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

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