API-36: 🚑 Fixing critical issue with the amount of sql connections (!15)
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:
2022-01-08 15:42:09 +00:00
parent 88569f8a40
commit b4d5bdd0b6
22 changed files with 159 additions and 580 deletions
+7 -47
View File
@@ -1,18 +1,10 @@
import * as dotenv from 'dotenv';
import {Vendor} from './vendor.interface';
import {Vendors} from './vendors.interface';
import {BetterzonDB} from '../Betterzon.db';
dotenv.config();
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.BETTERZON_DATABASE,
connectionLimit: 5
});
/**
* Data Model Interfaces
*/
@@ -26,10 +18,9 @@ const pool = mariadb.createPool({
* Fetches and returns all known vendors
*/
export const findAll = async (): Promise<Vendors> => {
let conn;
let conn = BetterzonDB.getConnection();
let vendorRows = [];
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE isActive = true');
for (let row in rows) {
if (row !== 'meta') {
@@ -59,10 +50,6 @@ export const findAll = async (): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return vendorRows;
@@ -73,10 +60,9 @@ export const findAll = async (): Promise<Vendors> => {
* @param id The id of the vendor to fetch
*/
export const find = async (id: number): Promise<Vendor> => {
let conn;
let conn = BetterzonDB.getConnection();
let vendor: any;
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE vendor_id = ? AND isActive = true', id);
for (let row in rows) {
if (row !== 'meta') {
@@ -86,10 +72,6 @@ export const find = async (id: number): Promise<Vendor> => {
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return vendor;
@@ -100,10 +82,9 @@ export const find = async (id: number): Promise<Vendor> => {
* @param term the term to match
*/
export const findBySearchTerm = async (term: string): Promise<Vendors> => {
let conn;
let conn = BetterzonDB.getConnection();
let vendorRows = [];
try {
conn = await pool.getConnection();
term = '%' + term + '%';
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE name LIKE ? AND isActive = true', term);
for (let row in rows) {
@@ -114,10 +95,6 @@ export const findBySearchTerm = async (term: string): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return vendorRows;
@@ -128,10 +105,9 @@ export const findBySearchTerm = async (term: string): Promise<Vendors> => {
* @param user The user to return the managed shops for
*/
export const getManagedShops = async (user_id: number): Promise<Vendors> => {
let conn;
let conn = BetterzonDB.getConnection();
let vendorRows = [];
try {
conn = await pool.getConnection();
const rows = await conn.query('SELECT vendor_id, name, streetname, zip_code, city, country_code, phone, website FROM vendors WHERE admin_id LIKE ?', user_id);
for (let row in rows) {
if (row !== 'meta') {
@@ -141,10 +117,6 @@ export const getManagedShops = async (user_id: number): Promise<Vendors> => {
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return vendorRows;
@@ -157,10 +129,8 @@ export const getManagedShops = async (user_id: number): Promise<Vendors> => {
* @param product_id The product id of the product to deactivate the listing for
*/
export const deactivateListing = async (user_id: number, vendor_id: number, product_id: number): Promise<Boolean> => {
let conn;
let conn = BetterzonDB.getConnection();
try {
conn = await pool.getConnection();
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
if (user_vendor_rows.length !== 1) {
@@ -172,10 +142,6 @@ export const deactivateListing = async (user_id: number, vendor_id: number, prod
return status.affectedRows > 0;
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return false;
@@ -188,10 +154,8 @@ export const deactivateListing = async (user_id: number, vendor_id: number, prod
* @param isActive The new active state
*/
export const setShopStatus = async (user_id: number, vendor_id: number, isActive: boolean): Promise<Boolean> => {
let conn;
let conn = BetterzonDB.getConnection();
try {
conn = await pool.getConnection();
// Check if the user is authorized to manage the requested vendor
const user_vendor_rows = await conn.query('SELECT vendor_id FROM vendors WHERE vendor_id = ? AND admin_id = ?', [vendor_id, user_id]);
if (user_vendor_rows.length !== 1) {
@@ -204,10 +168,6 @@ export const setShopStatus = async (user_id: number, vendor_id: number, isActive
return status.affectedRows > 0;
} catch (err) {
throw err;
} finally {
if (conn) {
conn.end();
}
}
return false;