Skip to content

Commit

Permalink
Merge pull request #721 from magieno/improve-mysql-client
Browse files Browse the repository at this point in the history
- Update in progress
  • Loading branch information
etiennenoel authored Aug 7, 2024
2 parents bce169a + c497cb4 commit 4213d74
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
32 changes: 31 additions & 1 deletion packages/mysql/src/clients/mysql.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class MysqlClient implements MysqlClientInterface {
* @param configUniqueKeyname
* @param force
*/
async getPool(configUniqueKeyname: string, force: boolean = false): Promise<Pool> {
async getPool(configUniqueKeyname: string, force: boolean = false, options = {}): Promise<Pool> {
if (!this.pools.has(configUniqueKeyname) && !force) {
try {
const mysqlConfig = await this.getMysqlConfig(configUniqueKeyname);
Expand All @@ -58,6 +58,7 @@ export class MysqlClient implements MysqlClientInterface {
password: mysqlConfig.password,
database: mysqlConfig.database,
debug: mysqlConfig.debug,
...options,
});

this.pools.set(configUniqueKeyname, pool);
Expand Down Expand Up @@ -195,6 +196,35 @@ export class MysqlClient implements MysqlClientInterface {

try {
const result = await pool.query(sqlStatement, values);

this.logHandler.debug("Successfully executed the SQL Statement", {sqlStatement, values, result})

return result[0];
} catch (error) {
this.logHandler.error("There was an error executing the SQL Statement", {
sqlStatement,
values,
error,
});

throw error;
}
}

/**
* This method returns the column name for a given class and property name.
* @param configUniqueKeyname
* @param sqlStatement
* @param values
*/
async querySql(configUniqueKeyname: string, sqlStatement: string, values: any[]): Promise<any> {
const pool = await this.getPool(configUniqueKeyname);

this.logHandler.debug("Executing SQL Statement", {sqlStatement, values});

try {
const result = await pool.query(sqlStatement, values);

this.logHandler.debug("Successfully executed the SQL Statement", {sqlStatement, values, result})

return result[0];
Expand Down
10 changes: 9 additions & 1 deletion packages/mysql/src/interfaces/mysql-client.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface MysqlClientInterface {
* @param configUniqueKeyname
* @param force
*/
getPool(configUniqueKeyname: string, force: boolean): Promise<Pool>;
getPool(configUniqueKeyname: string, force?: boolean, options?: any): Promise<Pool>;

/**
* This method returns the table metadata for a given class.
Expand Down Expand Up @@ -58,6 +58,14 @@ export interface MysqlClientInterface {
*/
executeSql(configUniqueKeyname: string, sqlStatement: string, values: any[]): Promise<any>

/**
* This method returns the column name for a given class and property name.
* @param configUniqueKeyname
* @param sqlStatement
* @param values
*/
querySql(configUniqueKeyname: string, sqlStatement: string, values: any[]): Promise<any>

/**
* This method maps the results to a given class type.
* @param classType
Expand Down

0 comments on commit 4213d74

Please sign in to comment.