-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseManager.js
44 lines (39 loc) · 1.6 KB
/
DatabaseManager.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
const mongoDb = require('mongodb');
module.exports = class DatabaseManager {
constructor(config) {
this.config = config;
this.raw = null;
this.db = null;
this.isDisabled = this.config.mongo_uri == null;
}
async init() {
if (this.isDisabled) return;
if (!this.config.mongo_uri) throw Error('Missing Mongo URI');
this.raw = await mongoDb.MongoClient.connect(this.config.mongo_uri, {}).catch(err => (console.error(err), null));
if (!this.raw) throw Error('Mongo URI Failed');
const urlTokens = /\w\/([^?]*)/g.exec(this.config.mongo_uri)
if (!urlTokens) throw Error('Missing Table Name');
this.db = this.raw.db(urlTokens && urlTokens[1]);
return true;
}
insertOne(collection, ...args) {
if (this.isDisabled) throw "[Function Disabled] Database URI was set to null.";
return this.db.collection(collection).insertOne(...args);
}
updateOne(collection, ...args) {
if (this.isDisabled) throw "[Function Disabled] Database URI was set to null.";
return this.db.collection(collection).updateOne(...args);
}
find(collection, ...args) {
if (this.isDisabled) throw "[Function Disabled] Database URI was set to null.";
return this.db.collection(collection).find(...args)?.toArray();
}
findOne(collection, ...args) {
if (this.isDisabled) throw "[Function Disabled] Database URI was set to null.";
return this.db.collection(collection).findOne(...args);
}
deleteOne(collection, ...args) {
if (this.isDisabled) throw "[Function Disabled] Database URI was set to null.";
return this.db.collection(collection).deleteOne(...args);
}
}