Skip to content

Commit df7a4ad

Browse files
DHAMODHARABALAJI RDHAMODHARABALAJI R
authored andcommitted
updated dbcontext
1 parent 80bdf8a commit df7a4ad

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

order-manager/src/handlers/createOrder.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
// const { setCache } = require("../utils/cache");
22
// const { sendToQueue } = require("../utils/sqs");
33
const { v4: uuidv4 } = require("uuid");
4-
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
5-
const { PutCommand, DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
6-
const client = new DynamoDBClient({});
7-
const docClient = DynamoDBDocumentClient.from(client);
8-
const ordersTable = "OrdersTable";
4+
const dbContext = require("../utils/dbContext");
5+
96

107

118
exports.handler = async (event) => {
129
try {
1310

14-
const { id, items } = event;
11+
const { items } = event;
1512

1613
if (!items || !items.length) {
1714
throw 'Items not configured in the payload'
@@ -22,12 +19,8 @@ exports.handler = async (event) => {
2219
items
2320
};
2421

25-
const command = new PutCommand({
26-
TableName: ordersTable,
27-
Item: order,
28-
});
22+
const response = await dbContext.Orders().Put(order);
2923

30-
const response = await docClient.send(command);
3124
console.log('order-created:', order, response);
3225

3326
// Payload Sample

order-manager/src/handlers/getMyOrders.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
2-
const { ScanCommand, DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
3-
const client = new DynamoDBClient({});
4-
const docClient = DynamoDBDocumentClient.from(client);
5-
const ordersTable = "OrdersTable";
1+
const dbContext = require("../utils/dbContext");
62

73
exports.handler = async (event) => {
84

9-
const body = await dynamo.send(
10-
new ScanCommand({
11-
TableName: ordersTable,
12-
})
13-
);
5+
const body = await dbContext.Orders().Scan();
146

157
console.log('items: ', body.Items)
168

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
2+
const { GetCommand, ScanCommand, PutCommand, DynamoDBDocumentClient } = require("@aws-sdk/lib-dynamodb");
3+
const client = new DynamoDBClient({});
4+
const docClient = DynamoDBDocumentClient.from(client);
5+
const ordersTable = "OrdersTable";
6+
7+
// console.log("🔒 HOSTING_ENV:", process.env.HOSTING_ENV);
8+
// console.log("🔒 AWS Region:", process.env.AWS_REGION);
9+
10+
const dbActions = (tableName) => {
11+
12+
const actions = {
13+
Get: async (whereClause) => {
14+
const command = new GetCommand({
15+
TableName: tableName,
16+
Key: whereClause
17+
});
18+
19+
const response = await docClient.send(command);
20+
return response.Item;
21+
},
22+
Put: async (obj) => {
23+
const command = new PutCommand({
24+
TableName: tableName,
25+
Item: obj,
26+
});
27+
28+
const response = await docClient.send(command);
29+
return response;
30+
},
31+
Scan: async () => {
32+
const body = await docClient.send(
33+
new ScanCommand({
34+
TableName: tableName,
35+
})
36+
);
37+
return body.Items;
38+
},
39+
};
40+
return (actions)
41+
42+
}
43+
44+
const dbContext = {
45+
46+
Orders: () => dbActions(ordersTable)
47+
48+
};
49+
50+
module.exports = dbContext;

0 commit comments

Comments
 (0)