This repository has been archived by the owner on Jun 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathddb.js
204 lines (181 loc) · 5.31 KB
/
ddb.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @Author: Jack Woods <jackrwoods>
* @Date: 2018-12-14T13:18:19-08:00
* @Filename: ddb.js
* @Last modified by: Brogan
* @Last modified time: 2019-03-26T11:43:00-07:00
* @Copyright: 2018 Oregon State University
*/
// Initialize AWS SDK
// On Linux, AWS loads user credentials from ~/.aws/credentials.
// For more information, take a look at https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html
var AWS = require('aws-sdk')
AWS.config.update({region: 'us-west-2'})
class DDBQuery {
constructor (ddb, table) {
this.ddb = ddb
this.table = table
}
select (params) {
params.TableName = this.table
return new Promise((resolve, reject) => {
this.ddb.query(params, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
}
scan (params) {
params.TableName = this.table
return new Promise((resolve, reject) => {
this.ddb.scan(params, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
}
update (params) {
params.TableName = this.table
return new Promise((resolve, reject) => {
this.ddb.update(params, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
}
put (params) {
params.TableName = this.table
return new Promise((resolve, reject) => {
this.ddb.put(params, (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
}
}
var state = {
ddb: null
}
exports.initialize = function () {
if (!state.ddb) {
state.ddb = new AWS.DynamoDB.DocumentClient()
}
}
exports.query = function (table) {
return new DDBQuery(state.ddb, table)
}
// This function querys the DynamoDB for the specified user's data.
exports.getUser = function (onid) {
// AWS SDK DDB Query Parameters - https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property
const params = {
'TableName': 'users',
'Select': 'ALL_ATTRIBUTES',
'Limit': 1,
'ConsistentRead': true,
'KeyConditionExpression': 'onid = :onid',
'ExpressionAttributeValues': {
':onid': onid
}
}
// Using a promise allows for promise chains.
return new Promise((resolve, reject) => {
state.ddb.query(params, function (err, data) {
if (err || data.Items.length === 0) { return reject(err) }
resolve(data.Items[0])
})
})
}
// Uploads a user to the db.
var putUser = function (usr) {
// AWS SDK DDB Query Parameters - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html
const params = {
'TableName': 'users',
'Item': usr
}
return new Promise((resolve, reject) => {
state.ddb.put(params, function (err, data) {
if (err) { return reject(err) }
resolve(data)
})
})
}
// This function compares the user object specified to the corresponding user item
// in the DynamoDB.
// - if the user does not exist, a new user is created with the given data.
// - if the user exists, then the function compares the user's data and
// updates data objects as necessary. Data objects in the database with
// the same date as new data objects will be updated.
exports.updateUser = function (usr) {
// Determine if the user exists.
this.getUser(usr.onid).then(function (dbUsr) {
// User exists.
// Map data elements to their dates
const dataMap = dbUsr.data.map(d => d.date)
// Iterate over the new data
usr.data.forEach(function (data, index, dataArr) {
// Search dataMap for the current data's date
let i = dataMap.indexOf(data.date)
// If the date is found, replace the data object with the most recent
if (i > -1) dbUsr.data.splice(i, 1, dataArr[index])
// Otherwise, add the data to the list
else dbUsr.data.push(dataArr[index])
// Upload the new user ddb item
putUser(dbUsr).catch((rej) => {
console.log(rej)
})
})
}).catch((rej) => {
// User does not exist.
// The user's ddb item will be created
putUser(usr).catch((rej) => {
console.log(rej)
})
})
}
exports.removeData = function (onid, did) {
// AWS SDK DDB Query Parameters - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html
const params = {
'TableName': 'users',
'Key': {
'onid': onid
},
'ExpressionAttributeNames': {
'#attribute': 'data'
},
'UpdateExpression': 'REMOVE #attribute['+ did +']',
'ReturnValue': 'UPDATED_NEW'
}
// Run query
return new Promise((resolve, reject) => {
state.ddb.update(params, function (err, data) {
if (err) {
return reject(err)
}
resolve(data)
})
})
}
// This function querys the DynamoDB for the CC questions.
exports.getQuestions = function () {
// AWS SDK DDB Scan Parameters - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
const params = {
'TableName': 'carbon-calculator-questions',
'Limit': 10,
'Select': 'ALL_ATTRIBUTES'
}
// Using a promise allows for promise chains.
return new Promise((resolve, reject) => {
state.ddb.scan(params, function (err, data) {
if (err || data.Items.length === 0) { return reject(err) }
resolve(data.Items)
})
})
}