feat: new DatabaseInstance class for universal database management, W.I.P
This commit is contained in:
parent
123fce455b
commit
b7de4d9614
5 changed files with 61 additions and 15 deletions
Structures/Classes
49
Structures/Classes/DatabaseInstance.js
Normal file
49
Structures/Classes/DatabaseInstance.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
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 };
|
Loading…
Add table
Add a link
Reference in a new issue