DEV Community

Cover image for Get All Items From DynamoDB Table with TypeScript
Rodion Chachura
Rodion 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.

Top comments (0)