From 5f1a9d0280a94b53069002f54a2220ade45ee00e Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 11:23:34 +0200 Subject: [PATCH 1/7] Add support for MSSQL named instances --- src/IConnectionOptions.ts | 2 ++ src/index.ts | 18 ++++++++++++++++++ src/templates/ormconfig.mst | 3 ++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index f7dec18..ddc71e4 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -15,6 +15,7 @@ export default interface IConnectionOptions { | "oracle" | "sqlite"; schemaName: string; + instanceName: string; ssl: boolean; skipTables: string[]; onlyTables: string[]; @@ -29,6 +30,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions { password: "", databaseType: undefined as any, schemaName: "", + instanceName: undefined as any, ssl: false, skipTables: [], onlyTables: [], diff --git a/src/index.ts b/src/index.ts index 122cbcd..9fcd9ba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -188,6 +188,12 @@ function checkYargsParameters(options: options): options { describe: "Schema name to create model from. Only for mssql and postgres. You can pass multiple values separated by comma eg. -s scheme1,scheme2,scheme3", }, + i: { + alias: "instance", + string: true, + default: options.connectionOptions.instanceName, + describe: "Named instance to create model from. Only for mssql.", + }, ssl: { boolean: true, default: options.connectionOptions.ssl, @@ -425,6 +431,18 @@ async function useInquirer(options: options): Promise { ]) ).schema; } + if (options.connectionOptions.databaseType === "mssql") { + options.connectionOptions.instanceName = ( + await inquirer.prompt([ + { + default: options.connectionOptions.instanceName, + message: "Named instance:", + name: "instanceName", + type: "input", + }, + ]) + ).instanceName; + } options.connectionOptions.port = parseInt(answ.port, 10); options.connectionOptions.host = answ.host; options.connectionOptions.user = answ.login; diff --git a/src/templates/ormconfig.mst b/src/templates/ormconfig.mst index 800022e..aa7f80f 100644 --- a/src/templates/ormconfig.mst +++ b/src/templates/ormconfig.mst @@ -7,7 +7,8 @@ "username": "{{user}}", "password": "{{password}}", "database": "{{databaseName}}",{{#schemaName}} - "schema": "{{.}}",{{/schemaName}} + "schema": "{{.}}",{{/schemaName}}{{#instanceName}} + "extra": { "instanceName": "{{.}}" },{{/instanceName}} "synchronize": false, "entities": [ "entities/*.js" From 00e44027e0bfa8fbb2c566f55cbb1dd108753a19 Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 11:32:42 +0200 Subject: [PATCH 2/7] Fix typo --- src/ModelGeneration.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index f9e3de4..2a544c4 100644 --- a/src/ModelGeneration.ts +++ b/src/ModelGeneration.ts @@ -278,10 +278,10 @@ function createTypeOrmConfig( ): void { const templatePath = path.resolve(__dirname, "templates", "ormconfig.mst"); const template = fs.readFileSync(templatePath, "utf-8"); - const compliedTemplate = Handlebars.compile(template, { + const compiledTemplate = Handlebars.compile(template, { noEscape: true, }); - const rendered = compliedTemplate(connectionOptions); + const rendered = compiledTemplate(connectionOptions); const formatted = Prettier.format(rendered, { parser: "json" }); const resultFilePath = path.resolve(outputPath, "ormconfig.json"); fs.writeFileSync(resultFilePath, formatted, { From ac6695123be8e64f4b51470a4be8505bbda30637 Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 11:44:59 +0200 Subject: [PATCH 3/7] Make instanceName optional --- src/IConnectionOptions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index ddc71e4..1cbea89 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -15,7 +15,7 @@ export default interface IConnectionOptions { | "oracle" | "sqlite"; schemaName: string; - instanceName: string; + instanceName?: string; ssl: boolean; skipTables: string[]; onlyTables: string[]; @@ -30,7 +30,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions { password: "", databaseType: undefined as any, schemaName: "", - instanceName: undefined as any, + instanceName: undefined, ssl: false, skipTables: [], onlyTables: [], From 6e9927fbf79f9cc82cdfe33e4cb2ba0cfcc84024 Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 12:16:25 +0200 Subject: [PATCH 4/7] Pass instanceName to driver --- src/drivers/MssqlDriver.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 01dcb66..8a87984 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -466,6 +466,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG options: { appName: "typeorm-model-generator", encrypt: connectionOptons.ssl, + instanceName: connectionOptons.instanceName, }, password: connectionOptons.password, port: connectionOptons.port, From 5f7bc2de7a46b30d78c056c205f3d853fdd39f32 Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 12:26:41 +0200 Subject: [PATCH 5/7] Fix yargs issue --- src/drivers/MssqlDriver.ts | 2 ++ src/index.ts | 1 + 2 files changed, 3 insertions(+) diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 8a87984..113433b 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -475,6 +475,8 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG user: connectionOptons.user, }; + console.log(config); + const promise = new Promise((resolve, reject) => { this.Connection = new this.MSSQL.ConnectionPool(config, (err) => { if (!err) { diff --git a/src/index.ts b/src/index.ts index 9fcd9ba..c883555 100644 --- a/src/index.ts +++ b/src/index.ts @@ -311,6 +311,7 @@ function checkYargsParameters(options: options): options { options.connectionOptions.schemaName = argv.s ? argv.s.toString() : standardSchema; + options.connectionOptions.instanceName = argv.i || undefined; options.connectionOptions.ssl = argv.ssl; options.connectionOptions.user = argv.u || standardUser; let skipTables = argv.skipTables.split(","); From 535d5b77ffc854ff94341f60c89606455b3e86a8 Mon Sep 17 00:00:00 2001 From: Frederik Hummel Date: Sat, 3 Oct 2020 12:37:45 +0200 Subject: [PATCH 6/7] Remove console.log --- src/drivers/MssqlDriver.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 113433b..8a87984 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -475,8 +475,6 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG user: connectionOptons.user, }; - console.log(config); - const promise = new Promise((resolve, reject) => { this.Connection = new this.MSSQL.ConnectionPool(config, (err) => { if (!err) { From 6423c0499f64b6646d6bb5e74201ccbe18325171 Mon Sep 17 00:00:00 2001 From: kononnable Date: Tue, 27 Oct 2020 18:28:34 +0100 Subject: [PATCH 7/7] don't ask for port number if instance name is specified --- src/index.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index c883555..7f1bd1c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -374,6 +374,19 @@ async function useInquirer(options: options): Promise { options.connectionOptions.schemaName = driver.standardSchema; } if (options.connectionOptions.databaseType !== "sqlite") { + if (options.connectionOptions.databaseType === "mssql") { + options.connectionOptions.instanceName = ( + await inquirer.prompt([ + { + default: options.connectionOptions.instanceName, + message: + "Instance name(leave empty if using port number):", + name: "instanceName", + type: "input", + }, + ]) + ).instanceName; + } const answ = await inquirer.prompt([ { default: options.connectionOptions.host, @@ -386,6 +399,7 @@ async function useInquirer(options: options): Promise { name: "port", type: "input", default: options.connectionOptions.port, + when: !options.connectionOptions.instanceName, validate(value) { const valid = !Number.isNaN(parseInt(value, 10)); return valid || "Please enter a valid port number"; @@ -432,18 +446,6 @@ async function useInquirer(options: options): Promise { ]) ).schema; } - if (options.connectionOptions.databaseType === "mssql") { - options.connectionOptions.instanceName = ( - await inquirer.prompt([ - { - default: options.connectionOptions.instanceName, - message: "Named instance:", - name: "instanceName", - type: "input", - }, - ]) - ).instanceName; - } options.connectionOptions.port = parseInt(answ.port, 10); options.connectionOptions.host = answ.host; options.connectionOptions.user = answ.login;