-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
87 lines (77 loc) · 1.69 KB
/
index.ts
File metadata and controls
87 lines (77 loc) · 1.69 KB
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
import {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Context,
} from "aws-lambda";
import { DynamoDB } from "aws-sdk";
const docClient = new DynamoDB.DocumentClient({
region: "eu-north-1",
});
/**
* GET handler, fetches all data for a partition key provided in lambda path parameters
*/
export const apiGetHandler = async (
event: APIGatewayProxyEvent,
_ctx: Context
): Promise<APIGatewayProxyResult> => {
const name = event.pathParameters?.name;
if (!name) {
return {
statusCode: 400,
body: JSON.stringify({
message: "Path parameter not provided",
}),
};
}
const { Items } = await docClient
.query({
TableName: process.env.TABLE_NAME!,
KeyConditionExpression: "PK = :pk",
ExpressionAttributeValues: {
":pk": name,
},
})
.promise();
console.log(Items);
return {
statusCode: 200,
body: JSON.stringify({
items: Items,
}),
};
};
/**
* POST handler, puts a new item to dynamodb
* partition key provided in path parameters, additional fields in body
*/
export const apiPostHandler = async (
event: APIGatewayProxyEvent,
_ctx: Context
): Promise<APIGatewayProxyResult> => {
const body = JSON.parse(event.body || "{}");
const name = event.pathParameters?.name;
if (!name) {
return {
statusCode: 400,
body: JSON.stringify({
message: "Path parameter not provided",
}),
};
}
await docClient
.put({
TableName: process.env.TABLE_NAME!,
Item: {
...body,
PK: name,
SK: new Date().toISOString(),
},
})
.promise();
return {
statusCode: 200,
body: JSON.stringify({
message: `OK`,
}),
};
};