-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.js
139 lines (132 loc) · 3.98 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
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
const { MongoClient, ObjectID } = require('mongodb'); // require the mongodb driver
/**
* Uses mongodb v3.6+ - [API Documentation](http://mongodb.github.io/node-mongodb-native/3.6/api/)
* 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) => {
db.collection('chatrooms').find().toArray((err, result) => {
if(err) return err;
else resolve(result);
});
})
)
}
Database.prototype.getRoom = function(room_id){
return this.connected.then(db =>
new Promise((resolve, reject) => {
if(ObjectID.isValid(room_id)){
db.collection('chatrooms').findOne({"_id": ObjectID(room_id)}).then((result) => {
resolve(result);
});
}
else if(typeof(room_id)==="string"){
db.collection('chatrooms').findOne({"_id":room_id}).then((result) => {
resolve(result);
})
}
else{
resolve(null);
}
})
)
}
Database.prototype.addRoom = function(room){
return this.connected.then(db =>
new Promise((resolve, reject) => {
if(room.name){
try {
db.collection('chatrooms').insertOne(room).then((result) => {
room._id=result.insertedId;
})
}
catch (e) {
throw (e);
}
resolve(room);
}
else{
reject(new Error("Invalid Room"));
}
})
)
}
Database.prototype.getLastConversation = function(room_id, before){
return this.connected.then(db =>
new Promise((resolve, reject) => {
if(!before){
before=Date.now();
}
db.collection('conversations').find({"room_id":room_id}).toArray((err, result) => {
if(err){
return err;
}
else{
var min=before;
var conversation = null;
result.forEach(element => {
if(before-element.timestamp<min && before-element.timestamp>0){
min=before-element.timestamp;
conversation=element;
}
});
resolve(conversation);
}
});
})
)
}
Database.prototype.addConversation = function(conversation){
return this.connected.then(db =>
new Promise((resolve, reject) => {
if(conversation.room_id && conversation.timestamp && conversation.messages){
try {
db.collection('conversations').insertOne(conversation).then((result) => {
conversation._id=result.insertedId;
})
}
catch (e) {
throw (e);
}
resolve(conversation);
}
else{
reject(new Error("Invalid Messages"));
}
})
)
}
Database.prototype.getUser = function(username){
return this.connected.then(db =>
new Promise((resolve, reject) => {
db.collection('users').findOne({"username":username}).then((result)=>{
resolve(result);
});
})
)
}
module.exports = Database;