diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index f7dec180..1cbea89f 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, ssl: false, skipTables: [], onlyTables: [], diff --git a/src/ModelGeneration.ts b/src/ModelGeneration.ts index f9e3de40..2a544c49 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, { diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 01dcb66f..8a879849 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, diff --git a/src/index.ts b/src/index.ts index 122cbcd1..7f1bd1c8 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, @@ -305,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(","); @@ -367,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, @@ -379,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"; diff --git a/src/templates/ormconfig.mst b/src/templates/ormconfig.mst index 800022ec..aa7f80fa 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"