-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesting_commands.js
More file actions
354 lines (318 loc) · 10 KB
/
testing_commands.js
File metadata and controls
354 lines (318 loc) · 10 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const {MongoClient, ObjectId} = require('mongodb');
const uri = "mongodb+srv://phoebe_bear:GoldenDragon1@comp30022-project.yybkyjm.mongodb.net/?retryWrites=true&w=majority"
const client = new MongoClient(uri);
async function create_users() {
MongoClient.connect(uri, function(err, db) {
var dbo = db.db("ProjectDatabase");
if (err) throw err;
var user_1 = { display_name: "Test User 1", login_email: "test_user1@gmail.com", hashed_password: "thisisapassword" };
var user_2 = { display_name: "Test User 2", login_email: "test_user3@gmail.com", hashed_password: "thisisapassword" };
var user_3 = { display_name: "Test User 3", login_email: "test_user3@gmail.com", hashed_password: "thisisapassword" };
var users = [user_1, user_2, user_3]
dbo.collection("users").insertMany(users, function(err, res) {
if (err) throw err;
console.log(res);
db.close();
});
});
}
async function get_users() {
var allValues;
try {
const database = client.db("ProjectDatabase");
const users = database.collection("users");
const cursor = users.find();
// print a message if no documents were found
if ((await cursor.countDocuments) == 0) {
console.log("No documents found!");
}
// replace console.dir with your callback to access individual elements
allValues = await cursor.toArray();
} finally {
await client.close();
}
return allValues
}
function call_back(input) {
console.log((input._id).toString())
}
async function main() {
users = await get_users().catch(console.dir);
test_owner = users[0]._id.toString()
console.log(users)
}
async function create_items(item) {
var result
try {
const database = client.db("ProjectDatabase");
const items = database.collection("items");
result = await items.insertOne(item);
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
await client.close();
}
return result._id
}
async function create_loan(loan) {
var result
try {
const database = client.db("ProjectDatabase");
const loans = database.collection("loans");
result = await loans.insertOne(loan);
console.log(`A document was inserted with the _id: ${result.insertedId}`);
} finally {
await client.close();
}
return result._id
}
async function alter_users() {
MongoClient.connect(uri, function(err, db) {
if (err) throw err;
const database = db.db("ProjectDatabase");
var myquery = {}
var newvalues = {
$set: { "item_categories": ["Electronics", "Books", "Stationary", "University Resources", "Cash", "Miscellaneous", "Personal", "Clothing and Apparel", "Toiletries and Beauty"] } }
const result = database.collection("users").updateMany(myquery, newvalues, function (err, res) {
if (err) throw err;
});
});
}
async function update_item(item_id) {
const database = client.db("ProjectDatabase");
const items = database.collection("items");
const filter = {_id: new ObjectId(item_id)}
const updateDocument = {
$set: {
being_loaned: true
},
$inc: {
loan_frequency: 1
}
}
const result = await items.updateOne(filter, updateDocument);
console.log(result)
}
const book = {
item_name: "Harry Potter",
category: "Books",
description: "The Philosopher's Stone.",
item_owner: new ObjectId("62fd8a9df04410afbc6df31d"),
being_loaned: false,
loan_frequency: 0
}
const laptop = {
item_name: "Macbook Pro",
category: "Electronics",
description: "Newest Macbook released by Apple",
item_owner: new ObjectId("62fd8a9df04410afbc6df31e"),
being_loaned: false,
loan_frequency: 0
}
const sunscreen = {
item_name: "Shiseido Sunscreen",
category: "Toiletries and Beauty",
description: "Very nice SPF 50+ sunscreen",
item_owner: new ObjectId("62fd8a9df04410afbc6df31d"),
being_loaned: false,
loan_frequency: 0
}
const loan_sunscreen = {
loaner_id: new ObjectId("62fd8a9df04410afbc6df31d"),
loanee_id: new ObjectId("62fd8a9df04410afbc6df31e"),
item_id: new ObjectId("62fd8d5bfd46ebc631b3bc79"),
status: "Current",
loan_start_date: new Date("2022-08-18"),
intended_return_date: new Date("2022-08-25"),
actual_return_date: new Date()
}
const loan_macbook = {
"loaner_id": "62fd8a9df04410afbc6df31e",
"loanee_id": "62fd8a9df04410afbc6df31d",
"item_id": "62fd8cf7fedbec68c80f5878",
"status": "Current",
"loan_start_date": "2022-08-18",
"intended_return_date": "2022-08-25"
}
<<<<<<< HEAD
loan_command = { collMod: "loans",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["loaner_id", "loanee_id", "item_id", "status", "loan_start_date", "intended_return_date"],
properties: {
loaner_id: {
bsonType: "objectId",
description: "loaner id."
},
loanee_id: {
bsonType: "objectId",
description: "loanee id."
},
item_id: {
bsonType: "objectId",
description: "item owner."
},
status: {
bsonType: "string",
enum: ["On Loan", "Overdue", "On Time Return", "Late Return", "Early Return"],
description: "loan status."
},
loan_start_date: {
bsonType: "date",
description: "Loan start date."
},
intended_return_date: {
bsonType: "date",
description: "Intended return date."
},
actual_return_date: {
bsonType: "date",
description: "Actual return date"
},
loanee_name: {
bsonType: "string",
description: "name"
},
item_image: {
bsonType: "string",
description: "item_image"
}
}
}
}
}
user_command = { collMod: "users",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["display_name", "login_email", "hashed_password", "item_categories"],
properties: {
display_name: {
bsonType: "string",
description: "display name"
},
login_email: {
bsonType: "string",
description: "email"
},
hashed_password: {
bsonType: "string",
description: "password"
},
item_categories: {
bsonType: "array",
description: "item categories",
items: {
bsonType: "string",
}
}
}
}
}
}
item_command = { collMod: "items",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["item_name", "category", "description", "item_owner", "being_loaned", "loan_frequency"],
properties: {
item_name: {
bsonType: "string",
description: "name"
},
category: {
bsonType: "string",
description: "category"
},
description: {
bsonType: "string",
description: "description"
},
item_owner: {
bsonType: "objectId",
description: "item_owner"
},
being_loaned: {
bsonType: "bool",
description: "being_loaned"
},
loan_frequency: {
bsonType: "int",
description: "loan_frequency"
},
image_url: {
bsonType: "string",
description: "image_url"
},
visible: {
bsonType: "bool",
description: "visible"
}
}
}
}
}
=======
>>>>>>> develop
async function main(){
await client.connect();
MongoClient.connect(uri, function(err, db) {
if (err) throw err;
<<<<<<< HEAD
db.db("ProjectDatabaseProduction").command(user_command, function(err, res) {
=======
db.db("ProjectDatabaseTesting").command( { collMod: "items",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["item_name", "category","item_owner", "being_loaned", "loan_frequency", "visible"],
properties: {
item_name: {
bsonType: "string",
description: "item_name"
},
category: {
bsonType: "string",
description: "category"
},
description: {
bsonType: "string",
description: "description"
},
item_owner: {
bsonType: "objectId",
description: "item_owner"
},
being_loaned: {
bsonType: "bool",
description: "being_loaned"
},
loan_frequency: {
bsonType: "int",
description: "loan_frequency"
},
visible: {
bsonType: "bool",
description: "visible"
},
image_url: {
bsonType: "string",
description: "image_url"
}
}
}
}
}, function(err, res) {
>>>>>>> develop
if (err) throw err;
console.log("Collection created!"); })
db.db("ProjectDatabaseProduction").command(loan_command, function(err, res) {
if (err) throw err;
console.log("Collection created!"); })
db.db("ProjectDatabaseProduction").command(item_command, function(err, res) {
if (err) throw err;
console.log("Collection created!"); })
return;
});
}
main()