-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries.js
121 lines (96 loc) · 3.28 KB
/
queries.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
process.env.AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
const AWS = require('aws-sdk')
const { createNorthwindTable } = require('./table/Northwind')
AWS.config.update({
region: 'local',
endpoint: 'http://localhost:8000',
accessKeyId: 'local',
secretAccessKey: 'local',
})
const start = async () => {
const documentClient = new AWS.DynamoDB.DocumentClient()
const table = createNorthwindTable(documentClient)
await getEmployeeById(table)
await getDirectReportsForAnEmployee(table)
await getDiscontinuedProducts(table)
await listAllOrdersOfAGivenProduct(table)
await getTheMostRecent25Orders(table)
await getShippersByName(table)
await getCustomersByContactName(table)
await listAllProductsIncludedInAnOrder(table)
await getSuppliersByCountryAndRegion(table)
}
const logResults = (description, results) => {
const border = '='.repeat(50)
const propertiesSorted = Object.keys(results.Items[0]).sort((a, z) =>
a.localeCompare(z),
)
console.log('\n')
console.log(border)
console.log(description)
console.log(border)
console.table(results.Items, propertiesSorted)
}
const getEmployeeById = async (table) => {
const results = await table.Employee.query('employeeID#2')
logResults('# a. Get employee by employee ID [employeeID=2]', results)
}
const getDirectReportsForAnEmployee = async (table) => {
const results = await table.Employee.query('employeeID#5', {
index: 'gsi_1',
})
logResults('# b. Get direct reports for an employee [employeeID=5]', results)
}
const getDiscontinuedProducts = async (table) => {
const results = await table.Product.query('PRODUCT', {
eq: '1',
index: 'gsi_1',
})
logResults('# c. Get discontinued products', results)
}
const listAllOrdersOfAGivenProduct = async (table) => {
const results = await table.OrderDetails.query('productID#1', {
index: 'gsi_1',
})
logResults('# d. List all orders of a given product [productID=1]', results)
}
const getTheMostRecent25Orders = async (table) => {
const results = await table.Order.query('ORDER', {
index: 'gsi_1',
limit: 25,
reverse: true,
})
logResults('# e. Get the most recent 25 orders', results)
}
const getShippersByName = async (table) => {
const results = await table.Shipper.query('United Package', {
index: 'gsi_1',
})
logResults('# f. Get shippers by name [name=United Package]', results)
}
const getCustomersByContactName = async (table) => {
const results = await table.Customer.query('Maria Anders', {
index: 'gsi_1',
})
logResults('# g. Get customers by contact name [name=Maria Anders]', results)
}
const listAllProductsIncludedInAnOrder = async (table) => {
const results = await table.OrderDetails.query('orderID#10260', {
beginsWith: 'productID#',
})
logResults('# h. List all products included in an order [orderID=10260]', results)
}
const getSuppliersByCountryAndRegion = async (table) => {
const results = await table.Supplier.query('SUPPLIER', {
index: 'gsi_1',
beginsWith: 'Germany#NULL',
})
logResults('# i. Get suppliers by country and region [Country=Germany]', results)
}
;(async () => {
try {
await start()
} catch (error) {
console.error(error)
}
})()