Created
January 13, 2024 13:44
-
-
Save proton0210/542e577b14e5898c6377307a52b06746 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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