-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderup-get.mjs
38 lines (34 loc) · 960 Bytes
/
orderup-get.mjs
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
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, ScanCommand, GetCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const dynamo = DynamoDBDocumentClient.from(client);
const tableName = 'OrderUpDB';
export const handler = async (event) => {
try {
let result;
if (event.pathParameters && event.pathParameters.id) {
const { id } = event.pathParameters;
result = await dynamo.send(
new GetCommand({
TableName: tableName,
Key: { id },
})
);
result = result.Item || {};
} else {
result = await dynamo.send(
new ScanCommand({ TableName: tableName })
);
result = result.Items || [];
}
return {
statusCode: 200,
body: JSON.stringify(result),
};
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify({ error: error.message }),
};
}
};