Skip to content

Commit

Permalink
MySQLConnector: Deno fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bleugateau committed Jun 19, 2021
1 parent ab81450 commit be20558
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/connectors/mysql-connector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { configMySQLLogger, MySQLClient, MySQLConnection, MySQLReponseTimeoutError } 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";
Expand All @@ -15,7 +20,7 @@ export interface MySQLOptions extends ConnectorOptions {
charset?: string;
logger?: LoggerConfig;
idleTimeout?: number;
reconnectOnTimeout?: boolean
reconnectOnTimeout?: boolean;
}

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

this._connected = true;
Expand All @@ -70,11 +75,10 @@ export class MySQLConnector implements Connector {
* Executing query
* @param queryDescription {QueryDescription}
* @param client {MySQLClient | MySQLConnection}
* @param reconnectAttempt {boolean} - Used for reconnect client when ResponseTimeoutError happened
*/
async query(
queryDescription: QueryDescription,
client?: MySQLClient | MySQLConnection
client?: MySQLClient | MySQLConnection,
): Promise<any | any[]> {
await this._makeConnection();

Expand All @@ -90,17 +94,17 @@ export class MySQLConnector implements Connector {

try {
result = await queryClient[queryMethod](subqueries[i]);
}
catch (error) {

} 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
if (
this._options.reconnectOnTimeout &&
error instanceof MySQLReponseTimeoutError
) {
await this.reconnect();

return this.query(queryDescription, client);
}

throw error;
}

Expand All @@ -114,13 +118,11 @@ export class MySQLConnector implements Connector {
return this._client.transaction(queries);
}


/**
* Reconnect client connection
*/
async reconnect() {
warning("Client reconnection has been triggered.");

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

0 comments on commit be20558

Please sign in to comment.