API-36: 🚑 Fixing critical issue with the amount of sql connections (!15)
Jenkins Production Deployment
Jenkins Production Deployment
Co-authored-by: Patrick Mueller <patrick@mueller-patrick.tech> Reviewed-on: #15 Co-authored-by: Patrick Müller <patrick@plutodev.de> Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit was merged in pull request #15.
This commit is contained in:
@@ -4,25 +4,10 @@ import {Guid} from 'guid-typescript';
|
||||
import {UserData} from './UserData.interface';
|
||||
import {Session} from './Session.interface';
|
||||
import {Status} from './Status.interface';
|
||||
import {PartyPlanerDB} from '../PartyPlaner.db';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const mariadb = require('mariadb');
|
||||
const prod_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_PROD_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
const dev_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_DEV_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
/**
|
||||
* Returns all data about the given user
|
||||
* @param useDev If the dev or prod database should be used
|
||||
@@ -30,14 +15,8 @@ const dev_pool = mariadb.createPool({
|
||||
* @return UserData An object containing the user data
|
||||
*/
|
||||
export const getUserData = async (useDev: boolean, userId: string): Promise<UserData> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
let rows = await conn.query('SELECT username, email, first_name, last_Name, last_login, email_is_verified, is_premium_user FROM users WHERE user_id = ?', userId);
|
||||
|
||||
let user: UserData = {} as UserData;
|
||||
@@ -57,10 +36,6 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
|
||||
return user;
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -70,14 +45,8 @@ export const getUserData = async (useDev: boolean, userId: string): Promise<User
|
||||
* @return any An object with a list of usernames and emails
|
||||
*/
|
||||
export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<any> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
const rows = await conn.query('SELECT username, email FROM users');
|
||||
|
||||
let usernames: string[] = [];
|
||||
@@ -94,10 +63,6 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
|
||||
};
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -113,14 +78,8 @@ export const getExistingUsernamesAndEmails = async (useDev: boolean): Promise<an
|
||||
* @param deviceInfo The user agent of the new user
|
||||
*/
|
||||
export const registerUser = async (useDev: boolean, username: string, email: string, firstName: string, lastName: string, password: string, ip: string, deviceInfo: string): Promise<Session> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
const pwHash = bcrypt.hashSync(password, 10);
|
||||
const sessionKey = Guid.create().toString();
|
||||
const sessionKeyHash = bcrypt.hashSync(sessionKey, 10);
|
||||
@@ -166,10 +125,6 @@ export const registerUser = async (useDev: boolean, username: string, email: str
|
||||
};
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -183,14 +138,8 @@ export const registerUser = async (useDev: boolean, username: string, email: str
|
||||
* @param deviceInfo The user agent of the new user
|
||||
*/
|
||||
export const loginUser = async (useDev: boolean, username: string, email: string, password: string, ip: string, deviceInfo: string): Promise<Session> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
let query_result;
|
||||
|
||||
// Get the saved hash
|
||||
@@ -248,10 +197,6 @@ export const loginUser = async (useDev: boolean, username: string, email: string
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -262,13 +207,8 @@ export const loginUser = async (useDev: boolean, username: string, email: string
|
||||
* @param email The email to check
|
||||
*/
|
||||
export const checkUsernameAndEmail = async (useDev: boolean, username: string, email: string): Promise<Status> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
const usernameQuery = 'SELECT username FROM users WHERE username = ?';
|
||||
const emailQuery = 'SELECT email FROM users WHERE email = ?';
|
||||
const usernameRes = await conn.query(usernameQuery, username);
|
||||
@@ -314,22 +254,12 @@ export const checkUsernameAndEmail = async (useDev: boolean, username: string, e
|
||||
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const checkSession = async (useDev: boolean, userId: string, sessionId: string, sessionKey: string): Promise<boolean> => {
|
||||
let conn;
|
||||
let conn = PartyPlanerDB.getConnection(useDev);
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
let rows = await conn.query('SELECT session_key_hash FROM sessions WHERE user_id = ? AND session_id = ?', [userId, sessionId]);
|
||||
|
||||
let savedHash = '';
|
||||
@@ -340,9 +270,5 @@ export const checkSession = async (useDev: boolean, userId: string, sessionId: s
|
||||
return bcrypt.compareSync(sessionKey, savedHash);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user