ExpressTemplate/Structures/Classes/DatabaseInstance.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

const mysql = require("mysql2");
const { readdir } = require("fs");
const path = require("path");
/**
* Universal database instance to be used for any database operations
*/
class DatabaseInstance {
/**
* Construct the DatabaseInstance using the database credentials
*
* @param {Object} credentials The database credentils in a standard mysql format
*/
constructor(credentials) {
this.pool = mysql.createPool({
database: credentials.database,
user: credentials.user,
password: credentials.password,
host: credentials.host,
port: credentials.port,
enableKeepAlive: true,
waitForConnections: true,
connectionLimit: 10
});
console.log("[JET » DatabaseInstance] Successfully initialized database connection")
this.pool.on("connection", (err) => {
if (err) console.log("[JET » DatabaseInstance] Error occurred during connection");
else console.log("[JET » DatabaseInstance » client] Successfully created new connection");
});
}
/**
*
* @param {String} query
* @param {Array} values
* @returns
*/
async execute(query, values, callback) {
this.pool.execute(query, values, callback);
}
async query(query, callback) {
this.pool.query(query, callback);
}
}
module.exports = { DatabaseInstance };