Skip to content
This repository has been archived by the owner on Oct 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #300 from Ricki-BumbleDev/mssql-named-instances
Browse files Browse the repository at this point in the history
Support MSSQL named instances
  • Loading branch information
Kononnable authored Oct 28, 2020
2 parents 4f3a034 + 6423c04 commit 1d60414
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/IConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default interface IConnectionOptions {
| "oracle"
| "sqlite";
schemaName: string;
instanceName?: string;
ssl: boolean;
skipTables: string[];
onlyTables: string[];
Expand All @@ -29,6 +30,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions {
password: "",
databaseType: undefined as any,
schemaName: "",
instanceName: undefined,
ssl: false,
skipTables: [],
onlyTables: [],
Expand Down
4 changes: 2 additions & 2 deletions src/ModelGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
1 change: 1 addition & 0 deletions src/drivers/MssqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(",");
Expand Down Expand Up @@ -367,6 +374,19 @@ async function useInquirer(options: options): Promise<options> {
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,
Expand All @@ -379,6 +399,7 @@ async function useInquirer(options: options): Promise<options> {
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";
Expand Down
3 changes: 2 additions & 1 deletion src/templates/ormconfig.mst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"username": "{{user}}",
"password": "{{password}}",
"database": "{{databaseName}}",{{#schemaName}}
"schema": "{{.}}",{{/schemaName}}
"schema": "{{.}}",{{/schemaName}}{{#instanceName}}
"extra": { "instanceName": "{{.}}" },{{/instanceName}}
"synchronize": false,
"entities": [
"entities/*.js"
Expand Down

0 comments on commit 1d60414

Please sign in to comment.