-
Notifications
You must be signed in to change notification settings - Fork 1
/
database_function.ts
34 lines (30 loc) · 1.06 KB
/
database_function.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
let AWS = require("aws-sdk");
//Configure AWS
AWS.config.update({
region: "us-east-1",
endpoint: "https://dynamodb.us-east-1.amazonaws.com"
});
//Create new DocumentClient
let documentClient = new AWS.DynamoDB.DocumentClient();
/* Function returns a Promise that will save the text with the specified id. */
export function saveData(tweetId: number, tweetDate: string, tweetText: string): Promise<string> {
//Table name and data for table
let params = {
TableName: "Twitter",
Item: {
id: tweetId,//Generated by Twitter
created_at: tweetDate, // created_at of tweet
text: tweetText,//Text of tweet
}
};
//Store data in DynamoDB and handle errors
return new Promise<string>((resolve, reject) => {
documentClient.put(params, (err, data) => {
if (err) {
reject("Unable to add item: " + JSON.stringify(err));
} else {
resolve("Item added to table with id: " + tweetId);
}
})
});
}