-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_collections.js
More file actions
135 lines (123 loc) · 5.83 KB
/
create_collections.js
File metadata and controls
135 lines (123 loc) · 5.83 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
const {MongoClient} = require('mongodb');
async function main(){
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const uri = "mongodb+srv://phoebe_bear:GoldenDragon1@comp30022-project.yybkyjm.mongodb.net/?retryWrites=true&w=majority"
const client = new MongoClient(uri);
try {
// Connect to the MongoDB cluster
await client.connect();
MongoClient.connect(uri, function(err, db) {
if (err) throw err;
var dbo = db.db("ProjectDatabase");
dbo.createCollection("users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["display_name", "login_email", "hashed_password"],
properties: {
display_name: {
bsonType: "string",
description: "Display name, for both internal and external use - is required."
},
login_email: {
bsonType: "string",
description: "Email (username), is required."
},
hashed_password: {
bsonType: "string",
description: "HASHED password of user - required for log in."
}
}
}
}
}, function(err, res) {
if (err) throw err;
console.log("Collection created!");
});
dbo.createCollection("items", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["item_name", "category", "description", "item_owner", "being_loaned", "loan_frequency"],
properties: {
item_name: {
bsonType: "string",
description: "Item name, user to input."
},
category: {
enum: ["Electronics", "Books", "Stationary", "University Resources", "Cash", "Miscellaneous", "Personal", "Clothing and Apparel", "Toiletries and Beauty"],
description: "Item must be one of these types."
},
description: {
bsonType: "string",
description: "Must be a string and is required. Can be empty string."
},
item_owner: {
bsonType: "objectId",
description: "A link to the owner of the item - required."
},
on_loan: {
bsonType: "bool",
description: "Boolean, whether or not item is currently on loan."
},
loan_count: {
bsonType: "int",
description: "Number of times item has been loaned out."
}
}
}
}
}, function(err, res) {
if (err) throw err;
console.log("Collection created!");
});
dbo.createCollection("loans", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["loaner_id", "loanee_id", "item_id", "status", "loan_start_date", "intended_return_date", "actual_return_date"],
properties: {
loaner_id: {
bsonType: "objectId",
description: "The Mongodb ID for the loaner user."
},
loanee_id: {
bsonType: "objectId",
description: "The Mongodb ID for the loanee user."
},
item_id: {
bsonType: "objectId",
description: "The Mongodb ID for the item."
},
status: {
enum: ["Current", "On Time Return", "Late Return", "Early Return"],
description: "Loan status, can only be one of these enum values."
},
loan_start_date: {
bsonType: "date",
description: "Start date."
},
intended_return_date: {
bsonType: "date",
description: "Desired end date."
},
actual_return_date: {
bsonType: "date",
description: "Actual end date."
}
}
}
}
}, function(err, res) {
if (err) throw err;
console.log("Collection created!");
});
});
} finally {
await client.close();
}
}
main().catch(console.error);