galactic-bot/storage/providers/Mariadb.js

73 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-04-11 21:16:37 +02:00
const Provider = require('../Provider.js');
2020-04-08 18:08:46 +02:00
const MySQL = require('mysql');
class MariaDBProvider extends Provider {
constructor(manager, config) {
2020-04-11 21:16:37 +02:00
super(manager, config, 'mariadb');
2020-04-08 18:08:46 +02:00
}
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.
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;