Skip to content

Commit

Permalink
MySQLConnector: Refactor many things
Browse files Browse the repository at this point in the history
  • Loading branch information
bleugateau committed Jun 16, 2021
1 parent 0d958d9 commit ab81450
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export {
configLogger as configMySQLLogger,
Connection as MySQLConnection,
} from "https://deno.land/x/[email protected]/mod.ts";

export {
ResponseTimeoutError as MySQLReponseTimeoutError
} from "https://deno.land/x/[email protected]/src/constant/errors.ts";

export type { LoggerConfig } from "https://deno.land/x/[email protected]/mod.ts";

export { Client as PostgresClient } from "https://deno.land/x/[email protected]/mod.ts";
Expand Down
39 changes: 21 additions & 18 deletions lib/connectors/mysql-connector.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { configMySQLLogger, MySQLClient, MySQLConnection } from "../../deps.ts";
import { configMySQLLogger, MySQLClient, MySQLConnection, MySQLReponseTimeoutError } from "../../deps.ts";
import type { LoggerConfig } from "../../deps.ts";
import type { Connector, ConnectorOptions } from "./connector.ts";
import { SQLTranslator } from "../translators/sql-translator.ts";
import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator.ts";
import type { QueryDescription } from "../query-builder.ts";
import { ResponseTimeoutError } from "https://deno.land/x/[email protected]/src/constant/errors.ts";
import { warning } from "../helpers/log.ts";

export interface MySQLOptions extends ConnectorOptions {
database: string;
Expand All @@ -15,6 +15,7 @@ export interface MySQLOptions extends ConnectorOptions {
charset?: string;
logger?: LoggerConfig;
idleTimeout?: number;
reconnectOnTimeout?: boolean
}

export class MySQLConnector implements Connector {
Expand Down Expand Up @@ -48,7 +49,7 @@ export class MySQLConnector implements Connector {
password: this._options.password,
port: this._options.port ?? 3306,
charset: this._options.charset ?? "utf8",
idleTimeout: this._options.idleTimeout ?? 4 * 3600 * 1000
idleTimeout: this._options.idleTimeout
});

this._connected = true;
Expand All @@ -73,8 +74,7 @@ export class MySQLConnector implements Connector {
*/
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection,
reconnectAttempt: boolean = true
client?: MySQLClient | MySQLConnection
): Promise<any | any[]> {
await this._makeConnection();

Expand All @@ -88,19 +88,20 @@ export class MySQLConnector implements Connector {
for (let i = 0; i < subqueries.length; i++) {
let result = null;

try{
try {
result = await queryClient[queryMethod](subqueries[i]);
}
catch(e){
//prevent unhandled behavior
if(!(e instanceof ResponseTimeoutError) || !reconnectAttempt){
return result;
catch (error) {

//reconnect client on timeout error
if (this._options.reconnectOnTimeout && error instanceof MySQLReponseTimeoutError) {
//reconnect client, at this moment we can't subscribe to connectionState of mysql driver, we need to do this
await this.reconnect();

return this.query(queryDescription, client);
}

//reconnect client, at this moment we can't subscribe to connectionState of mysql driver, we need to do this
await this.reconnect();

return await this.query(queryDescription, client, false);

throw error;
}

if (i === subqueries.length - 1) {
Expand All @@ -113,11 +114,13 @@ export class MySQLConnector implements Connector {
return this._client.transaction(queries);
}


/**
* Reconnect client by close existing and reconnecting it
* Reconnect client connection
*/
async reconnect(){
console.log('Debug >> Reconnect MySQL Client');
async reconnect() {
warning("Client reconnection has been triggered.");

await this.close();
return this._makeConnection();
}
Expand Down

0 comments on commit ab81450

Please sign in to comment.