0

This question references the method examples in AWS SDK for Javascript documentation.

The examples on the page (ie. Describing a table) splits up the code into modules:

  1. ddbClient.js (has configuration settings).
  2. ddb_describetable.js (has code that actually runs the method).

In ddb_describetable.js, why is there an export in front of the "const params..." and the "const run = async"? Can you explain the purpose with a high level example/best practices in relation to AWS or nodejs (if examples/best practices apply)?

ddbClient.js

// Create service client module using ES6 syntax.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
// Set the AWS Region.
const REGION = "REGION"; //e.g. "us-east-1"
// Create an Amazon DynamoDB service client object.
const ddbClient = new DynamoDBClient({ region: REGION });
export { ddbClient };

ddb_describetable.js

// Import required AWS SDK clients and commands for Node.js
import { DescribeTableCommand } from "@aws-sdk/client-dynamodb";
import { ddbClient } from "./libs/ddbClient.js";

// Set the parameters
export const params = { TableName: "TABLE_NAME" }; //TABLE_NAME

export const run = async () => {
  try {
    const data = await ddbClient.send(new DescribeTableCommand(params));
    console.log("Success", data);
    // console.log("Success", data.Table.KeySchema);
    return data;
  } catch (err) {
    console.log("Error", err);
  }
};
run();

The actual DescribeTableCommand code documentation (which doesn't show an example) doesn't imply that this structure should be used.

tdev
  • 45
  • 1
  • 3
  • 9
  • 1
    You export something when you want to import it in other modules. You don't when you don't. There's not really much behind this, and it has nothing to do with AWS. – Bergi Mar 21 '22 at 01:12
  • @Bergi, so is this export just called 'run'? Is that what I'd import? Seems like they would've named it something specific to the Describe Table method. – tdev Mar 21 '22 at 03:08
  • Yes, they're exporting `run` and `params`. However, immediately calling `run` is weird, as is [the error handling](https://stackoverflow.com/q/50896442/1048572). – Bergi Mar 21 '22 at 08:59

0 Answers0