-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_fns.js
37 lines (30 loc) · 1.06 KB
/
db_fns.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
class BotDatabaseFunctions {
constructor(config) {
// Load config
this.mongo_url = config.mongo_url;
this.mongo_db_name = config.mongo_db_name;
// Collection names
this.setting_collection = 'bot_settings';
}
// Executes a database transaction with MongoDB
async mongoTransaction(coll_name, callback) {
const Client = require('mongodb').MongoClient(this.mongo_url, { useUnifiedTopology: true });
var res = [];
try {
await Client.connect();
const database = Client.db(this.mongo_db_name);
const collection = database.collection(coll_name);
res = await callback(collection);
} finally {
await Client.close();
}
return res;
}
// Finds data from a collection
async find(coll_name, filters) {
return await this.mongoTransaction(coll_name, async function(coll) {
return await coll.find(filters).toArray();
});
}
}
module.exports = BotDatabaseFunctions;