-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
242 lines (222 loc) · 6.19 KB
/
loader.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const AWS = require("aws-sdk")
AWS.config.update({ region: "us-east-1" })
const db = new AWS.DynamoDB()
const async = require("async")
var users = [["mickey mouse", "password", "mickey", "mouse", "[email protected]", "upenn", "10-14-2002", ["news", "sports"]]]
var initTable = function (key, tableName, callback) {
db.listTables(function (err, data) {
if (err) {
console.log(err, err.stack)
callback("Error when listing tables: " + err, null)
} else {
console.log("Connected to AWS DynamoDB")
var tables = data.TableNames.toString().split(",")
console.log("Tables in DynamoDB: " + tables)
if (tables.indexOf(tableName) == -1) {
console.log("Creating new table '" + tableName + "'")
var params = {
AttributeDefinitions: [
{
AttributeName: key,
AttributeType: "S",
},
],
KeySchema: [
{
AttributeName: key,
KeyType: "HASH",
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 20, // DANGER: Don't increase this too much; stay within the free tier!
WriteCapacityUnits: 20, // DANGER: Don't increase this too much; stay within the free tier!
},
TableName: tableName /* required */,
}
db.createTable(params, function (err, data) {
if (err) {
console.log(err)
callback("Error while creating table " + tableName + ": " + err, null)
} else {
console.log("Table is being created; waiting for 20 seconds...")
setTimeout(function () {
console.log("Success")
callback(null, "Success")
}, 20000)
}
})
} else {
console.log("Table " + tableName + " already exists")
callback(null, "already created")
}
}
})
}
initTable("username", "users", function (err, data) {
if (err) {
console.log("error making table users")
} else {
if (data === "already created") {
console.log("Table users already exists - adding dummy values")
async.forEach(
users,
function (user, callback) {
console.log("Uploading user: " + user[0])
var params = {
Item: {
username: {
S: user[0],
},
password: {
S: user[1],
},
first_name: {
S: user[2],
},
last_name: {
S: user[3],
},
email: {
S: user[4],
},
affiliation: {
S: user[5],
},
birthday: {
S: user[6],
},
interests: {
SS: user[7],
},
},
TableName: "users",
ReturnValues: "NONE",
}
db.putItem(params, function (err, data) {
if (err) callback(err)
else callback(null, "Success")
})
},
function () {
console.log("Upload complete")
}
)
} else {
console.log("Successfully created table users")
}
}
})
initTable("sender", "friends", function (err, data) {
if (err) {
console.log("error making table friends")
} else {
if (data === "already created") {
console.log("Table friends already exists - adding dummy values")
const friends = [
["jren2", "test1", 0],
["test1", "test2", 1],
["test2", "test1", 1],
]
async.forEach(
friends,
function (friend, callback) {
console.log("Uploading friend: " + friend[0])
var params = {
Item: {
sender: {
S: friend[0],
},
receiver: {
S: friend[1],
},
status: {
S: friend[2],
},
},
TableName: "friends",
ReturnValues: "NONE",
}
db.putItem(params, function (err, data) {
if (err) callback(err)
else callback(null, "Success")
})
},
function () {
console.log("Upload complete")
}
)
} else {
console.log("Successfully created table friends")
}
}
})
initTable("post_id", "posts", function (err, data) {
if (err) {
console.log("error making table posts")
} else {
if (data === "already created") {
console.log("Table prefixes already exists - adding dummy values")
const prefixes = [
["j", ["jren2"]],
["jr", ["jren2"]],
["jre", ["jren2"]],
["jren", ["jren2"]],
["jren2", ["jren2"]],
]
async.forEach(
prefixes,
function (prefix, callback) {
console.log("Uploading prefix: " + prefixes[0])
var params = {
Item: {
prefix: {
S: prefix[0],
},
usernames: {
SS: prefix[1],
},
},
TableName: "prefixes",
}
db.putItem(params, function (err, data) {
if (err) callback(err)
else callback(null, "Success")
})
},
function () {
console.log("Upload complete")
}
)
} else {
console.log("Successfully created table users")
}
}
})
initTable("prefix", "prefixes", function (err, data) {
if (err) {
console.log("error making table prefixes")
} else {
console.log("successfully created table prefixes")
}
})
initTable("group_id", "groups", function (err, data) {
if (err) {
console.log("error making table groups")
} else {
console.log("successfully created table groups")
}
})
initTable("sender_uid", "messages", function (err, data) {
if (err) {
console.log("error making table messages")
} else {
console.log("successfully created table messages")
}
})
initTable("news_id", "news", function (err, data) {
if (err) {
console.log("error making table news")
} else {
console.log("successfully created table news")
}
})