Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws-sdk : DynamoDB : Fetch list of all tables

I am using aws-sdk for nodejs in a project, and not finding any way to fetch all tables from an endpoint, in other words I am looking forward for equivalent of following command in SDK.

aws dynamodb list-tables --endpoint-url <ENDPOINT_URL>

Any help is much appreciated.

Regards.

like image 986
Alok G. Avatar asked May 12 '18 13:05

Alok G.


People also ask

What is the list in DynamoDB table?

Returns an array of table names associated with the current account and endpoint. The output from ListTables is paginated, with each page returning a maximum of 100 table names.

Can DynamoDB store lists?

DynamoDB supports the Java Set , List , and Map collection types. The following table summarizes how these Java types map to the DynamoDB types.

Does DynamoDB query return all items?

The Query operation in Amazon DynamoDB finds items based on primary key values. You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value.


2 Answers

If you have more than 100 tables, then you also need to be aware of pagination. You can use this example for Node.js:

const readAllTables = async() => {
  const AWS = require("aws-sdk");
  var dynamodb = new AWS.DynamoDB();

  var params = {};
  var tables = [];

  while(true) {
    var response = await dynamodb.listTables(params).promise();
    tables = tables.concat(response.TableNames);

    if (undefined === response.LastEvaluatedTableName) {
      break;
    } else {
      params.ExclusiveStartTableName = response.LastEvaluatedTableName;
    }
  }

  return tables;
}
like image 58
Jaxt0r Avatar answered Nov 16 '22 02:11

Jaxt0r


Thanks friends, I found the solution as

 const AWS = require("aws-sdk");
 var dynamodb = new AWS.DynamoDB();
 var param = {}
    dynamodb.listTables(param, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });
like image 37
Alok G. Avatar answered Nov 16 '22 01:11

Alok G.