-
Notifications
You must be signed in to change notification settings - Fork 29
/
Database.js
76 lines (69 loc) · 2.09 KB
/
Database.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
const { MongoClient, ObjectID } = require('mongodb'); // require the mongodb driver
/**
* Uses mongodb v4.2+ - [API Documentation](http://mongodb.github.io/node-mongodb-native/4.2/)
* Database wraps a mongoDB connection to provide a higher-level abstraction layer
* for manipulating the objects in our cpen322 app.
*/
function Database(mongoUrl, dbName){
if (!(this instanceof Database)) return new Database(mongoUrl, dbName);
this.connected = new Promise((resolve, reject) => {
MongoClient.connect(
mongoUrl,
{
useNewUrlParser: true
},
(err, client) => {
if (err) reject(err);
else {
console.log('[MongoClient] Connected to ' + mongoUrl + '/' + dbName);
resolve(client.db(dbName));
}
}
)
});
this.status = () => this.connected.then(
db => ({ error: null, url: mongoUrl, db: dbName }),
err => ({ error: err })
);
}
Database.prototype.getRooms = function(){
return this.connected.then(db =>
new Promise((resolve, reject) => {
/* TODO: read the chatrooms from `db`
* and resolve an array of chatrooms */
})
)
}
Database.prototype.getRoom = function(room_id){
return this.connected.then(db =>
new Promise((resolve, reject) => {
/* TODO: read the chatroom from `db`
* and resolve the result */
})
)
}
Database.prototype.addRoom = function(room){
return this.connected.then(db =>
new Promise((resolve, reject) => {
/* TODO: insert a room in the "chatrooms" collection in `db`
* and resolve the newly added room */
})
)
}
Database.prototype.getLastConversation = function(room_id, before){
return this.connected.then(db =>
new Promise((resolve, reject) => {
/* TODO: read a conversation from `db` based on the given arguments
* and resolve if found */
})
)
}
Database.prototype.addConversation = function(conversation){
return this.connected.then(db =>
new Promise((resolve, reject) => {
/* TODO: insert a conversation in the "conversations" collection in `db`
* and resolve the newly added conversation */
})
)
}
module.exports = Database;