Get All Items From DynamoDB Table with TypeScript

radzion

Radzion Chachura

Posted on January 7, 2023

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.

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
radzion
Radzion Chachura

Posted on January 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About