-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MySQLConnector: Refactor many things
- Loading branch information
1 parent
0d958d9
commit ab81450
Showing
2 changed files
with
26 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -15,6 +15,7 @@ export interface MySQLOptions extends ConnectorOptions { | |
charset?: string; | ||
logger?: LoggerConfig; | ||
idleTimeout?: number; | ||
reconnectOnTimeout?: boolean | ||
} | ||
|
||
export class MySQLConnector implements Connector { | ||
|
@@ -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; | ||
|
@@ -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(); | ||
|
||
|
@@ -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) { | ||
|
@@ -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(); | ||
} | ||
|