Merge pull request #300 from Ricki-BumbleDev/mssql-named-instances

Support MSSQL named instances
This commit is contained in:
Paweł Kotarski 2020-10-28 21:54:05 +01:00 committed by GitHub
commit 1d60414a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 3 deletions

View File

@ -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: [],

View File

@ -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, {

View File

@ -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,

View File

@ -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> {
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<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";

View File

@ -7,7 +7,8 @@
"username": "{{user}}",
"password": "{{password}}",
"database": "{{databaseName}}",{{#schemaName}}
"schema": "{{.}}",{{/schemaName}}
"schema": "{{.}}",{{/schemaName}}{{#instanceName}}
"extra": { "instanceName": "{{.}}" },{{/instanceName}}
"synchronize": false,
"entities": [
"entities/*.js"