Categories
JavaScript Answers

How to fetch/scan all items from AWS dynamodb using Node.js?

Spread the love

To fetch/scan all items from AWS dynamodb using Node.js, we use the scan method.

For instance, we write

const scanTable = async (tableName) => {
  const params = {
    TableName: tableName,
  };

  const scanResults = [];
  const items;
  do {
    items = await documentClient.scan(params).promise();
    items.Items.forEach((item) => scanResults.push(item));
    params.ExclusiveStartKey = items.LastEvaluatedKey;
  } while (typeof items.LastEvaluatedKey !== "undefined");

  return scanResults;
};

to call scan to scan the database.

We call promise to return a promise.

And we use await to get the results.

We use items.Items to get the items returned.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.