-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.js
124 lines (122 loc) · 4.73 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
const mongo = require('mongodb');
const MongoClient = mongo.MongoClient;
const DB_PASS = process.env.DB_PASS || require('./ignore/secret.json').DB_PASS;
module.exports = {
name: 'mongodb',
description: 'Connect to MongoDB',
client: null,
connect() {
const uri = `${DB_PASS}?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {useNewUrlParser: true, useUnifiedTopology: true});
return new Promise((resolve, reject) =>
client.connect(error => {
if (error) {
client.close();
reject('Unable to connect to database', error);
} else {
console.log('MongoDB Connected!');
resolve(client);
}
})
);
},
getClient() {
return new Promise((resolve, reject) => {
if (!module.exports.client)
module.exports.connect()
.then(client => {
module.exports.client = client;
resolve(client)
})
.catch(error => reject(error));
else resolve(module.exports.client);
});
},
find(tableName, value) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).findOne(value, function (err, result) {
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
},
findAll(tableName, value) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).find(value).toArray(function (err, result) {
if (err) reject(err);
else {
let A = [];
result.forEach(D => A.push(D));
let U = [...new Set(A)]
resolve(U);
}
})
).catch(error => reject(error))
);
},
findAndUpdate(tableName, query, update, options) {
return module.exports.getClient().then(client =>
client.db("zello").collection(tableName).findOneAndUpdate(query, update, options)
.then(updatedDocument => {
return updatedDocument;
}));
},
addOrUpdate(tableName, id, command) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).updateOne(id, command, {upsert: true}, function (err, result) {
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
},
add(tableName, id, command) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).insertOne(id, command, function (err, result) {
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
},
average(tableName, queryId, amountId) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).aggregate({
$group: {
"_id": queryId,
"avg": {$avg: {$toInt: amountId}}
}
}, function (err, result) {
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
},
mostPopular(tableName, queryId) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).aggregate({$sortByCount: queryId}, function (err, result) {
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
},
groupAndSort(tableName, command) {
return new Promise((resolve, reject) =>
module.exports.getClient().then(client =>
client.db("zello").collection(tableName).aggregate({$group: command}, {$sort: {count: -1}}).toArray(function (err, result) { //TODO: MAP LIST OF COMMANDS
if (err) reject(err);
else resolve(result);
})
).catch(error => reject(error))
);
}
}