Skip to content

Commit 2aad063

Browse files
committed
Refactor
1 parent c91d7a0 commit 2aad063

File tree

5 files changed

+89
-70
lines changed

5 files changed

+89
-70
lines changed

lambda/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export const multiTableHandler = async (event: any) => {
1515
};
1616

1717
export const datagenHandler = async () => {
18-
const numberCustomers = 10;
18+
const numberCustomers = 60;
1919
return RandomDataGenerator.loadFakeData(numberCustomers);
2020
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Customer } from "../models";
2+
import { CustomerDynamoDBItem } from "./dynamo-models";
3+
4+
function convertCustomerItem(item: CustomerDynamoDBItem): Customer {
5+
return {
6+
customerId: item.pk.substring("Customer#".length),
7+
name: item.name,
8+
lastName: item.lastName,
9+
isPrime: item.isPrime,
10+
address: item.address,
11+
orders: [],
12+
};
13+
}
14+
15+
function toCustomerItem(customer: Customer): CustomerDynamoDBItem {
16+
return {
17+
pk: `Customer#${customer.customerId}`,
18+
sk: `Customer#${customer.customerId}`,
19+
type: "Customer",
20+
name: customer.name,
21+
lastName: customer.lastName,
22+
address: customer.address,
23+
isPrime: customer.isPrime,
24+
};
25+
}
26+
27+
export default {
28+
convertCustomerItem,
29+
toCustomerItem,
30+
};

lambda/single-table/dynamo-models.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
interface DynamoDBItem {
2+
pk: string;
3+
sk: string;
4+
type: string;
5+
}
6+
7+
export interface CustomerDynamoDBItem extends DynamoDBItem {
8+
name: string;
9+
lastName: string;
10+
isPrime: boolean;
11+
address: {
12+
street: string;
13+
city: string;
14+
state: string;
15+
country: string;
16+
};
17+
}
18+
19+
export interface OrderDynamoDBItem extends DynamoDBItem {
20+
date: string;
21+
total: number;
22+
}

lambda/single-table/index.ts

Lines changed: 10 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { DocumentClient } from "aws-sdk/clients/dynamodb";
22
import { Customer, Order } from "../models";
3+
import { CustomerDynamoDBItem, OrderDynamoDBItem } from "./dynamo-models";
4+
import CustomerFactory from "./customer-factory";
5+
import OrderFactory from "./order-factory";
36

47
const dynamodb = new DocumentClient();
58
const SINGLE_TABLE = process.env.SINGLE_TABLE || "";
@@ -25,17 +28,20 @@ async function fetchCustomerWithOrders(customerId: string): Promise<Customer> {
2528
const customerItem = result.Items.find((item) => item.type === "Customer");
2629
const orderItems = result.Items.filter((item) => item.type === "Order");
2730

28-
const customer = convertCustomerItem(customerItem as CustomerDynamoDBItem);
31+
const customer = CustomerFactory.convertCustomerItem(
32+
customerItem as CustomerDynamoDBItem
33+
);
34+
2935
const orders = orderItems.map((i) =>
30-
convertOrderItem(i as OrderDynamoDBItem)
36+
OrderFactory.convertOrderItem(i as OrderDynamoDBItem)
3137
);
3238

3339
customer.orders = orders;
3440
return customer;
3541
}
3642

3743
async function saveCustomer(customer: Customer) {
38-
const item = toCustomerItem(customer);
44+
const item = CustomerFactory.toCustomerItem(customer);
3945
return dynamodb
4046
.put({
4147
TableName: SINGLE_TABLE,
@@ -45,7 +51,7 @@ async function saveCustomer(customer: Customer) {
4551
}
4652

4753
async function saveOrder(order: Order) {
48-
const item = toOrderItem(order);
54+
const item = OrderFactory.toOrderItem(order);
4955
return dynamodb
5056
.put({
5157
TableName: SINGLE_TABLE,
@@ -54,71 +60,6 @@ async function saveOrder(order: Order) {
5460
.promise();
5561
}
5662

57-
interface DynamoDBItem {
58-
pk: string;
59-
sk: string;
60-
type: string;
61-
}
62-
63-
interface CustomerDynamoDBItem extends DynamoDBItem {
64-
name: string;
65-
lastName: string;
66-
isPrime: boolean;
67-
address: {
68-
street: string;
69-
city: string;
70-
state: string;
71-
country: string;
72-
};
73-
}
74-
75-
interface OrderDynamoDBItem extends DynamoDBItem {
76-
date: string;
77-
total: number;
78-
}
79-
80-
function convertCustomerItem(item: CustomerDynamoDBItem): Customer {
81-
return {
82-
customerId: item.pk.substring("Customer#".length),
83-
name: item.name,
84-
lastName: item.lastName,
85-
isPrime: item.isPrime,
86-
address: item.address,
87-
orders: [],
88-
};
89-
}
90-
91-
function convertOrderItem(item: OrderDynamoDBItem): Order {
92-
return {
93-
orderId: item.sk.substring("Order#".length),
94-
customerId: item.pk.substring("Customer#".length),
95-
date: new Date(item.date),
96-
total: item.total,
97-
};
98-
}
99-
100-
function toCustomerItem(customer: Customer): CustomerDynamoDBItem {
101-
return {
102-
pk: `Customer#${customer.customerId}`,
103-
sk: `Customer#${customer.customerId}`,
104-
type: "Customer",
105-
name: customer.name,
106-
lastName: customer.lastName,
107-
address: customer.address,
108-
isPrime: customer.isPrime,
109-
};
110-
}
111-
112-
function toOrderItem(order: Order): OrderDynamoDBItem {
113-
return {
114-
pk: `Customer#${order.customerId}`,
115-
sk: `Order#${order.orderId}`,
116-
type: "Order",
117-
total: order.total,
118-
date: order.date.toISOString(),
119-
};
120-
}
121-
12263
export default {
12364
fetchCustomerWithOrders,
12465
saveCustomer,

lambda/single-table/order-factory.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Order } from "../models";
2+
import { OrderDynamoDBItem } from "./dynamo-models";
3+
4+
function convertOrderItem(item: OrderDynamoDBItem): Order {
5+
return {
6+
orderId: item.sk.substring("Order#".length),
7+
customerId: item.pk.substring("Customer#".length),
8+
date: new Date(item.date),
9+
total: item.total,
10+
};
11+
}
12+
13+
function toOrderItem(order: Order): OrderDynamoDBItem {
14+
return {
15+
pk: `Customer#${order.customerId}`,
16+
sk: `Order#${order.orderId}`,
17+
type: "Order",
18+
total: order.total,
19+
date: order.date.toISOString(),
20+
};
21+
}
22+
23+
export default {
24+
convertOrderItem,
25+
toOrderItem,
26+
};

0 commit comments

Comments
 (0)