generated from kawarimidoll/deno-dev-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynamodb.ts
97 lines (84 loc) · 2.05 KB
/
dynamodb.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {
DeleteItemCommand,
DynamoDBClient,
GetItemCommand,
PutItemCommand,
} from "./deps.ts";
const accessKeyId = Deno.env.get("AWS_ACCESS_KEY_ID") || "dummy-id";
const secretAccessKey = Deno.env.get("AWS_SECRET_ACCESS_KEY") || "dummy-key";
if (accessKeyId === "dummy-id" || secretAccessKey === "dummy-key") {
console.warn("missing credentials. starts with dummy values.");
}
const client = new DynamoDBClient({
region: "us-east-1",
credentials: { accessKeyId, secretAccessKey },
});
const tableName = "Denote";
export interface DenoteSchema {
name: string;
hashedToken: string;
config: string;
}
export async function putItem(data: DenoteSchema) {
try {
const response = await client.send(
new PutItemCommand({
TableName: tableName,
Item: {
// Here 'S' implies that the value is of type string
name: { S: data.name },
hashedToken: { S: data.hashedToken },
config: { S: data.config },
},
}),
);
console.log(response);
const { $metadata: { httpStatusCode } } = response;
return httpStatusCode === 200;
} catch (error) {
console.log(error);
}
return false;
}
export async function getItem(name: string) {
try {
const response = await client.send(
new GetItemCommand({
TableName: tableName,
Key: {
name: { S: name },
},
}),
);
console.log(response);
const { Item } = response;
if (Item) {
return {
name: Item.name.S,
hashedToken: Item.hashedToken.S,
config: Item.config.S,
};
}
} catch (error) {
console.log(error);
}
return null;
}
export async function deleteItem(name: string) {
try {
const response = await client.send(
new DeleteItemCommand({
TableName: tableName,
Key: {
name: { S: name },
},
}),
);
console.log(response);
const { $metadata: { httpStatusCode } } = response;
return httpStatusCode === 200;
} catch (error) {
console.log(error);
}
return false;
}