From ecd90e8ac8899b915b8dcb5677057c0108d08bb4 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Fri, 9 Aug 2019 22:34:56 +0200 Subject: [PATCH] query timeout option #185 #188 --- src/IConnectionOptions.ts | 1 + src/drivers/MssqlDriver.ts | 1 + src/drivers/MysqlDriver.ts | 2 ++ src/drivers/PostgresDriver.ts | 1 + src/index.ts | 39 +++++++++++++++++++++++++++++++---- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index 65dee14..6da7f91 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -7,4 +7,5 @@ export class IConnectionOptions { public databaseType: string = ""; public schemaName: string = ""; public ssl: boolean = false; + public timeout?: number; } diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 75701c2..4331b08 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -405,6 +405,7 @@ order by }, password: connectionOptons.password, port: connectionOptons.port, + requestTimeout: connectionOptons.timeout, server: connectionOptons.host, user: connectionOptons.user }; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 88e6156..f618e34 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -377,6 +377,7 @@ export class MysqlDriver extends AbstractDriver { ssl: { rejectUnauthorized: false }, + timeout: connectionOptons.timeout, user: connectionOptons.user }; } else { @@ -385,6 +386,7 @@ export class MysqlDriver extends AbstractDriver { host: connectionOptons.host, password: connectionOptons.password, port: connectionOptons.port, + timeout: connectionOptons.timeout, user: connectionOptons.user }; } diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 4e4afee..04ac1d0 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -569,6 +569,7 @@ WHERE "n"."nspname" = table_schema AND "t"."typname"=udt_name password: connectionOptons.password, port: connectionOptons.port, ssl: connectionOptons.ssl, + statement_timeout: connectionOptons.timeout, user: connectionOptons.user }); diff --git a/src/index.ts b/src/index.ts index af4bee5..1361a20 100644 --- a/src/index.ts +++ b/src/index.ts @@ -161,6 +161,10 @@ function GetUtilParametersByArgs() { boolean: true, default: false, describe: "Generate constructor allowing partial initialization" + }) + .option("timeout", { + describe: "SQL Query timeout(ms)", + number: true }).argv; const driver = createDriver(argv.e); @@ -184,6 +188,7 @@ function GetUtilParametersByArgs() { ? argv.s.toString() : standardSchema), (connectionOptions.ssl = argv.ssl), + (connectionOptions.timeout = argv.timeout), (connectionOptions.user = argv.u ? argv.u.toString() : standardUser); const generationOptions: IGenerationOptions = new IGenerationOptions(); (generationOptions.activeRecord = argv.a), @@ -306,15 +311,41 @@ async function GetUtilParametersByInquirer() { type: "input" } ])) as any).output; - const customize = ((await inquirer.prompt([ + + if ( + connectionOptions.databaseType === "mssql" || + connectionOptions.databaseType === "postgres" + ) { + const changeRequestTimeout = ((await inquirer.prompt([ + { + default: false, + message: "Do you want to change default sql query timeout?", + name: "changeRequestTimeout", + type: "confirm" + } + ])) as any).changeRequestTimeout; + if (changeRequestTimeout) { + const timeout: any = ((await inquirer.prompt({ + message: "Query timeout(ms):", + name: "timeout", + type: "input", + validate(value) { + const valid = !isNaN(parseInt(value, 10)); + return valid || "Please enter a valid number"; + } + })) as any).timeout; + connectionOptions.timeout = timeout; + } + } + const customizeGeneration = ((await inquirer.prompt([ { default: false, message: "Do you want to customize generated model?", - name: "customize", + name: "customizeGeneration", type: "confirm" } - ])) as any).customize; - if (customize) { + ])) as any).customizeGeneration; + if (customizeGeneration) { const customizations: string[] = ((await inquirer.prompt([ { choices: [