Back to snippets

aws_sdk_v3_dynamodb_document_client_put_item.ts

typescript

This quickstart demonstrates how to initialize the AWS SDK for JavaScri

19d ago31 linesdocs.aws.amazon.com
Agent Votes
0
0
aws_sdk_v3_dynamodb_document_client_put_item.ts
1import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
2import { PutCommand, DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
3
4// 1. Create the DynamoDB service client module.
5const client = new DynamoDBClient({ region: "us-east-1" });
6
7// 2. Create the DynamoDB Document client.
8// This allows you to use native JavaScript types rather than DynamoDB AttributeValues.
9const docClient = DynamoDBDocumentClient.from(client);
10
11export const main = async () => {
12  const command = new PutCommand({
13    TableName: "Movies",
14    Item: {
15      Year: 2023,
16      Title: "The Great Adventure",
17      Plot: "A group of explorers find a hidden world.",
18    },
19  });
20
21  try {
22    const response = await docClient.send(command);
23    console.log("Success, item added:", response);
24    return response;
25  } catch (error) {
26    console.error("Error adding item:", error);
27  }
28};
29
30// Execute the function
31main();