0
0
DynamoDBquery~5 mins

AWS SDK for JavaScript/Node.js in DynamoDB

Choose your learning style9 modes available
Introduction

The AWS SDK for JavaScript/Node.js helps you talk to AWS services like DynamoDB easily from your JavaScript code.

You want to save or read data from a DynamoDB database in your Node.js app.
You need to automate AWS tasks like uploading files to S3 or sending messages with SNS.
You want to build a web app that interacts with AWS services without manual setup.
You want to write scripts that manage AWS resources programmatically.
You want to connect your backend code to AWS services securely and efficiently.
Syntax
DynamoDB
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({ region: "us-east-1" });

const command = new PutItemCommand({
  TableName: "YourTableName",
  Item: {
    "PrimaryKey": { S: "keyValue" },
    "Attribute": { S: "value" }
  }
});

async function run() {
  const response = await client.send(command);
  console.log(response);
}

run();

Use import to bring in AWS SDK modules.

Create a client for the AWS service you want to use, like DynamoDBClient.

Examples
This example reads an item with UserId '123' from the 'Users' table.
DynamoDB
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({ region: "us-west-2" });

const command = new GetItemCommand({
  TableName: "Users",
  Key: {
    "UserId": { S: "123" }
  }
});

async function run() {
  const response = await client.send(command);
  console.log(response.Item);
}

run();
This example deletes an item with OrderId 'order123' from the 'Orders' table.
DynamoDB
import { DynamoDBClient, DeleteItemCommand } from "@aws-sdk/client-dynamodb";

const client = new DynamoDBClient({ region: "eu-central-1" });

const command = new DeleteItemCommand({
  TableName: "Orders",
  Key: {
    "OrderId": { S: "order123" }
  }
});

async function run() {
  await client.send(command);
  console.log('Item deleted successfully');
}

run();
Sample Program

This program adds an item with Id '001' and Name 'Alice' to the DynamoDB table named 'TestTable'. It prints success or error messages.

DynamoDB
import { DynamoDBClient, PutItemCommand } from "@aws-sdk/client-dynamodb";

async function addItem() {
  const client = new DynamoDBClient({ region: "us-east-1" });

  const command = new PutItemCommand({
    TableName: "TestTable",
    Item: {
      "Id": { S: "001" },
      "Name": { S: "Alice" }
    }
  });

  try {
    const response = await client.send(command);
    console.log("Item added successfully", response);
  } catch (error) {
    console.error("Error adding item", error);
  }
}

addItem();
OutputSuccess
Important Notes

Always specify the AWS region when creating the client.

Use async/await to handle asynchronous calls cleanly.

Catch errors to handle failures gracefully.

Summary

The AWS SDK for JavaScript/Node.js lets you work with AWS services in your code.

Create a client, build a command, and send it to perform actions.

Use async/await and error handling for smooth operation.