tm-playwright-framework
Version:
Playwright Cucumber TS framework - The easiest way to learn
109 lines (108 loc) • 3.68 kB
JavaScript
/**
* CUSTOM.TS
*
* @author Sasitharan, Govindharam
* @reviewer Sahoo, AshokKumar
* @version 1.0 - 1st-JUNE-2025
*
* @description
* TBD
*
* @methods
* TBD - TBD
*/
// db.ts
import mysql from 'mysql2/promise';
import Logger from "tm-playwright-framework/dist/report/logger.js";
import path from 'path';
import fs from "fs-extra";
let pool, config = {
host: undefined,
user: undefined,
password: undefined,
database: undefined
};
async function initPool() {
if (pool) {
return pool;
}
const configPath = path.join(process.cwd(), 'config.json');
if (fs.existsSync(configPath)) {
try {
const fileContent = fs.readFileSync(configPath, 'utf-8');
config = JSON.parse(fileContent);
}
catch (error) {
Logger.logStatus("Failed", `Error reading config file: ${error.message}`);
throw error;
}
}
else {
console.log("Config file not found, using environment variables or defaults.");
console.log("Add config.json to the root directory for MySql configurations as follows: {host: undefined, user: undefined, password: undefined, database: undefined }");
Logger.logStatus("Info", "Config file not found, using environment variables or defaults.");
Logger.logStatus("Info", "Add config.json to the root directory for MySql configurations as follows: {host: undefined, user: undefined, password: undefined, database: undefined }");
}
const host = process.env.MYSQL_HOST || config.host;
const user = process.env.MYSQL_USER || config.user;
const password = process.env.MYSQL_PASSWORD || config.password;
const database = process.env.MYSQL_DATABASE || config.database;
pool = mysql.createPool({
host,
user,
password,
database,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
return pool;
}
async function fetchCred(columnToFetch, table, conditionalColumn) {
const query = `SELECT ?? FROM ?? WHERE CredentialID LIKE ?;`;
pool = await initPool();
try {
const [rows] = await pool.query(query, [columnToFetch, table, conditionalColumn]);
return rows[0]?.[`${columnToFetch}`];
}
catch (error) {
Logger.logStatus("Failed", `Error fetching credentials: ${error.message}`);
throw error;
}
}
async function executeQuery(columnToFetch, filterBy, table = process.env.MYSQL_TABLE) {
const query = `SELECT ?? FROM ?? WHERE CredentialID LIKE ?;`;
const mySqlPool = await initPool();
filterBy = filterBy.replace("*", "").replace("%", "").replace(" or ", "");
try {
const [rows] = await mySqlPool.query(query, [columnToFetch, table, filterBy]);
Logger.logStatus("Success", `Successfully executed the query`);
return rows;
}
catch (error) {
Logger.logStatus("Failed", `Failed to execute the query: ${error.message}`);
throw error;
}
}
export async function getCredential(columnToFetch, filterBy, table = process.env.MYSQL_TABLE || 'mcs_credentials') {
const result = await executeQuery(columnToFetch, filterBy, table);
if (result) {
return result[0]?.[`${columnToFetch}`];
}
else {
return null;
}
}
process.on('SIGINT', async () => {
if (pool) {
try {
await pool.end();
Logger.logStatus("Success", "MySQL connection pool closed.");
process.exit(0);
}
catch (error) {
Logger.logStatus("Failed", `Error closing MySQL connection pool: ${error.message}`);
process.exit(1);
}
}
});