forked from Galactic/galactic-bot
73 lines
1.6 KiB
JavaScript
73 lines
1.6 KiB
JavaScript
const Provider = require('../Provider.js');
|
|
const MySQL = require('mysql');
|
|
|
|
class MariaDBProvider extends Provider {
|
|
|
|
constructor(manager, config) {
|
|
|
|
super(manager, config, 'mariadb');
|
|
|
|
}
|
|
|
|
async init() {
|
|
|
|
try {
|
|
|
|
this.db = MySQL.createPool(this.config);
|
|
|
|
this.db.on('connection', async connection => {
|
|
|
|
// this.manager.logger.log('MariaDB connected.');
|
|
|
|
connection.on('error', err => {
|
|
// this.manager.logger.error('MariaDB errored.', err);
|
|
});
|
|
|
|
connection.on('close', data => {
|
|
// this.manager.logger.log('MariaDB connection closed.', data);
|
|
})
|
|
|
|
});
|
|
|
|
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.
|
|
* @param {array<Object>} values Array of values to replace ? with in the query string
|
|
* @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; |