galactic-bot/structure/storage/providers/Mongodb.js
2020-08-17 23:25:17 +03:00

76 lines
1.6 KiB
JavaScript

const { Provider } = require('../interfaces/');
const { MongoClient } = require('mongodb');
class MongoDBProvider extends Provider {
constructor(client, config) {
super(client, {
name: 'mongodb',
config
});
}
async initialize() {
try {
this.connection = await MongoClient.connect(this.config.host+this.config.database, { useUnifiedTopology: true });
this.db = await this.connection.db(this.config.database);
return this._initialized = true; //eslint-disable-line no-return-assign
} catch(err) {
this._error(err);
return false;
}
}
stats(options = {}) {
return new Promise((resolve, reject) => {
if (!this._initialized) return reject(new Error('MongoDB is not connected.'));
this.db.stats(options, (error, result) => {
if (error) reject(error);
resolve(result);
});
});
}
get guilds() {
return this.tables.guilds;
}
get permissions() {
return this.tables.permissions;
}
get infractions() {
return this.tables.infractions;
}
get messages() {
return this.tables.messages;
}
get attachments() {
return this.tables.attachments;
}
get users() {
return this.tables.users;
}
get role_cache() {
return this.tables.role_cache;
}
get webhooks() {
return this.tables.webhooks;
}
}
module.exports = MongoDBProvider;