forked from aws-samples/aws-sdk-js-notes-app
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlistNotes.ts
30 lines (27 loc) · 996 Bytes
/
listNotes.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
import { DynamoDBClient, ScanCommand } from "@aws-sdk/client-dynamodb";
import { unmarshall } from "@aws-sdk/util-dynamodb";
import { success, failure } from "./libs/response";
export const handler = async () => {
const params = {
TableName: process.env.NOTES_TABLE_NAME || "",
};
try {
let client: DynamoDBClient;
if (process.env.LOCALSTACK_HOSTNAME) {
const localStackConfig = {
endpoint: `http://${process.env.LOCALSTACK_HOSTNAME}:${process.env.EDGE_PORT}`,
region: "us-east-1", // Change the region as per your setup
};
client = new DynamoDBClient(localStackConfig);
} else {
// Use the default AWS configuration
client = new DynamoDBClient({});
}
const result = await client.send(new ScanCommand(params));
// Return the matching list of items in response body
return success(result.Items.map((Item) => unmarshall(Item)));
} catch (e) {
console.log(e);
return failure({ status: false });
}
};