galactic-bot/structure/storage/providers/Mariadb.js

76 lines
1.6 KiB
JavaScript
Raw Normal View History

const { Provider } = require('../interfaces/');
2020-04-08 18:08:46 +02:00
const MySQL = require('mysql');
class MariaDBProvider extends Provider {
constructor(client, config) {
2020-04-08 18:08:46 +02:00
super(client, {
name: 'mariadb',
config
});
2020-04-08 18:08:46 +02:00
}
async initialize() {
2020-04-08 18:08:46 +02:00
try {
this.db = MySQL.createPool(this.config);
this.db.on('connection', async (connection) => {
2020-04-08 18:08:46 +02:00
// this.manager.logger.log('MariaDB connected.');
connection.on('error', (err) => {
2020-04-08 18:08:46 +02:00
// this.manager.logger.error('MariaDB errored.', err);
});
connection.on('close', (data) => {
2020-04-08 18:08:46 +02:00
// this.manager.logger.log('MariaDB connection closed.', data);
});
2020-04-08 18:08:46 +02:00
});
this.loaded = true;
} catch(err) {
// this.manager.logger.error('MariaDB connection failed.', err);
}
return this;
}
close() {
this.db.end();
}
/**
* Query using SQL to MariaDB
*
* @param {string} query SQL query string.
2020-04-11 21:16:37 +02:00
* @param {array<Object>} values Array of values to replace ? with in the query string
2020-04-08 18:08:46 +02:00
* @returns {object} Returns an object containing the query result
* @memberof MariaDBProvider
*/
query(query, values) {
if(!this.loaded) throw new Error('MariaDB not connected');
return new Promise((resolve, reject) => {
this.db.query(query, values, (err, result) => {
if(err) reject(err);
resolve(result);
});
});
}
}
module.exports = MariaDBProvider;