-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo.ts
47 lines (44 loc) · 1.07 KB
/
dynamo.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
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
DynamoDB,
GetItemCommand,
GetItemCommandInput,
PutItemCommand,
PutItemCommandInput,
} from '@aws-sdk/client-dynamodb';
const dynamoDB = new DynamoDB({});
const dynamoDBTable = process.env.dynamoDBTable;
export async function getState(): Promise<string> {
const params: GetItemCommandInput = {
Key: {
key: { S: 'state' },
},
TableName: dynamoDBTable,
};
const getItemCommand = new GetItemCommand(params);
return await dynamoDB
.send(getItemCommand)
.then((res) => {
if (res.Item !== undefined) {
return res.Item.value.S;
} else {
return 'not found';
}
})
.catch((err) => {
throw err;
});
}
export async function setState(state: string) {
const params: PutItemCommandInput = {
Item: {
key: { S: 'state' },
timestamp: { N: String(Math.floor(Date.now() / 1000)) },
value: { S: state },
},
TableName: dynamoDBTable,
};
const putItemCommand = new PutItemCommand(params);
await dynamoDB.send(putItemCommand).catch((err) => {
console.log(err);
});
}