Skip to content

Instantly share code, notes, and snippets.

@proton0210
Created January 13, 2024 13:44
Show Gist options
  • Save proton0210/542e577b14e5898c6377307a52b06746 to your computer and use it in GitHub Desktop.
Save proton0210/542e577b14e5898c6377307a52b06746 to your computer and use it in GitHub Desktop.
import * as dotenv from "dotenv";
dotenv.config();
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { marshall } from "@aws-sdk/util-dynamodb";
const region_name = "ap-south-1";
// Initialize DynamoDB client
const dynamodb = new DynamoDB({
region: region_name,
});
const table_name = "Flights";
// Sample data
const data = [
{
FlightID: "FL123",
Origin: "Mumbai",
Destination: "Los Angeles",
DepartureTime: "2023-07-01T08:00:00",
ArrivalTime: "2023-07-01T11:00:00",
},
{
FlightID: "FL456",
Origin: "Bengaluru",
Destination: "Miami",
DepartureTime: "2023-07-02T09:30:00",
ArrivalTime: "2023-07-02T12:30:00",
},
];
// Insert data into DynamoDB table
const putItems = async () => {
for (const item of data) {
const params = {
TableName: table_name,
Item: marshall(item),
};
try {
await dynamodb.putItem(params);
console.log(`Successfully added item: ${JSON.stringify(item)}`);
} catch (error: any) {
console.error(
`Error adding item: ${JSON.stringify(item)} - ${error.message}`
);
}
}
};
putItems();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment