Switch to pooling for db connections

This commit is contained in:
Skylar Grant 2021-11-29 20:05:39 -05:00
parent e67fd83b4d
commit 2b904f6a88
1 changed files with 3 additions and 23 deletions

View File

@ -26,13 +26,15 @@ const dotCommandFiles = fs.readdirSync('./dot-commands/').filter(file => file.en
// MySQL // MySQL
const mysql = require('mysql'); const mysql = require('mysql');
const db = new mysql.createConnection({ const db = new mysql.createPool({
connectionLimit: 10,
host: dbHost, host: dbHost,
user: dbUser, user: dbUser,
password: dbPass, password: dbPass,
database: dbName, database: dbName,
port: dbPort, port: dbPort,
}); });
db.connect();
const functions = { const functions = {
collections: { collections: {
@ -331,82 +333,65 @@ const functions = {
upload: { upload: {
request(commandData, client) { request(commandData, client) {
const query = `INSERT INTO requests (author, request, status) VALUES ('${commandData.author}','${commandData.args}','Active')`; const query = `INSERT INTO requests (author, request, status) VALUES ('${commandData.author}','${commandData.args}','Active')`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.download.requests(client); functions.download.requests(client);
}); });
db.end();
}, },
pasta(pastaData, client) { pasta(pastaData, client) {
const query = `INSERT INTO pastas (name, content) VALUES ('${pastaData.name}','${pastaData.content}')`; const query = `INSERT INTO pastas (name, content) VALUES ('${pastaData.name}','${pastaData.content}')`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.download.pastas(client); functions.download.pastas(client);
}); });
db.end();
}, },
joint(content, client) { joint(content, client) {
const query = `INSERT INTO joints (content) VALUES ('${content}')`; const query = `INSERT INTO joints (content) VALUES ('${content}')`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.download.joints(client); functions.download.joints(client);
}); });
db.end();
}, },
gif(gifData, client) { gif(gifData, client) {
const query = `INSERT INTO gifs (name, embed_url) VALUES ('${gifData.name}', '${gifData.embed_url}')`; const query = `INSERT INTO gifs (name, embed_url) VALUES ('${gifData.name}', '${gifData.embed_url}')`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.download.gifs(client); functions.download.gifs(client);
}); });
db.end();
} }
}, },
download: { download: {
requests(client) { requests(client) {
const query = 'SELECT * FROM requests WHERE status = \'Active\' ORDER BY id ASC'; const query = 'SELECT * FROM requests WHERE status = \'Active\' ORDER BY id ASC';
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.collections.requests(rows, client); functions.collections.requests(rows, client);
}); });
db.end();
}, },
pastas(client) { pastas(client) {
const query = 'SELECT * FROM pastas ORDER BY id ASC'; const query = 'SELECT * FROM pastas ORDER BY id ASC';
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.collections.pastas(rows, client); functions.collections.pastas(rows, client);
}); });
db.end();
}, },
gifs(client) { gifs(client) {
const query = 'SELECT * FROM gifs ORDER BY id ASC'; const query = 'SELECT * FROM gifs ORDER BY id ASC';
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.collections.gifs(rows, client); functions.collections.gifs(rows, client);
}); });
db.end();
}, },
joints(client) { joints(client) {
const query = 'SELECT * FROM joints ORDER BY id ASC'; const query = 'SELECT * FROM joints ORDER BY id ASC';
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.collections.joints(rows, client); functions.collections.joints(rows, client);
}); });
db.end();
}, },
strain(commandData, message) { strain(commandData, message) {
const { strainName } = commandData; const { strainName } = commandData;
const query = `SELECT id, name, type, effects, ailment, flavor FROM strains WHERE name = '${strainName}'`; const query = `SELECT id, name, type, effects, ailment, flavor FROM strains WHERE name = '${strainName}'`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (rows != undefined) { if (rows != undefined) {
commandData.strainInfo = { commandData.strainInfo = {
@ -420,16 +405,13 @@ const functions = {
functions.embeds.strain(commandData, message); functions.embeds.strain(commandData, message);
} }
}); });
db.end();
}, },
strains(client) { strains(client) {
const query = 'SELECT id, name FROM strains'; const query = 'SELECT id, name FROM strains';
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.collections.strains(rows, client); functions.collections.strains(rows, client);
}); });
db.end();
}, },
}, },
weed: { weed: {
@ -451,12 +433,10 @@ const functions = {
// Parent-Level functions (miscellaneuous) // Parent-Level functions (miscellaneuous)
closeRequest(requestId, client) { closeRequest(requestId, client) {
const query = `UPDATE requests SET status = 'Closed' WHERE id = ${requestId}`; const query = `UPDATE requests SET status = 'Closed' WHERE id = ${requestId}`;
db.connect();
db.query(query, (err, rows, fields) => { db.query(query, (err, rows, fields) => {
if (err) throw err; if (err) throw err;
functions.download.requests(client); functions.download.requests(client);
}); });
db.end();
}, },
spongebob(commandData) { spongebob(commandData) {
let flipper = 0; let flipper = 0;