diff --git a/src/Engine.ts b/src/Engine.ts index bf4efe6..671ef82 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -144,6 +144,7 @@ function addImportsAndGenerationOptions( }); element.GenerateConstructor = generationOptions.generateConstructor; element.IsActiveRecord = generationOptions.activeRecord; + element.detached = generationOptions.detached; element.Imports.filter((elem, index, self) => { return index === self.indexOf(elem); }); diff --git a/src/IGenerationOptions.ts b/src/IGenerationOptions.ts index 11a0016..4e2a061 100644 --- a/src/IGenerationOptions.ts +++ b/src/IGenerationOptions.ts @@ -11,4 +11,5 @@ export class IGenerationOptions { public generateConstructor: boolean = false; public customNamingStrategyPath: string = ""; public relationIds: boolean = false; + public detached: boolean = false; } diff --git a/src/entity.mst b/src/entity.mst index 5a49072..371ce0e 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -3,7 +3,7 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne {{/each}} -@Entity("{{sqlEntityName}}"{{#Schema}},{schema:"{{.}}"{{#if ../Database}}, database:"{{../Database}}"{{/if}} } {{/Schema}}) +@Entity("{{sqlEntityName}}"{{#unless detached}}{{#Schema}} ,{schema:"{{.}}"{{#if ../Database}}, database:"{{../Database}}"{{/if}} } {{/Schema}}{{/unless}}) {{#Indexes}}{{^isPrimaryKey}}@Index("{{name}}",[{{#columns}}"{{toPropertyName name}}",{{/columns}}]{{#isUnique}},{unique:true}{{/isUnique}}) {{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName tsEntityName}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} { {{#Columns}} diff --git a/src/index.ts b/src/index.ts index a87cb02..04ed2b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -157,6 +157,12 @@ function GetUtilParametersByArgs() { default: false, describe: "Generate RelationId fields" }) + .option("detached", { + boolean: true, + default: false, + describe: + "If set, omits database and schema identifier in generated entities" + }) .option("generateConstructor", { boolean: true, default: false, @@ -196,6 +202,7 @@ function GetUtilParametersByArgs() { (generationOptions.noConfigs = argv.noConfig), (generationOptions.propertyVisibility = argv.pv), (generationOptions.relationIds = argv.relationIds), + (generationOptions.detached = argv.detached), (generationOptions.resultsPath = argv.o ? argv.o.toString() : null); return { driver, connectionOptions, generationOptions }; @@ -339,6 +346,11 @@ async function GetUtilParametersByInquirer() { name: "Generate RelationId fields", value: "relationId" }, + { + name: + "Omit schema qualifier, allowing you to switch it at runtime", + value: "detached" + }, { name: "Generate constructor allowing partial initialization", @@ -360,6 +372,7 @@ async function GetUtilParametersByInquirer() { "activeRecord" ); generationOptions.relationIds = customizations.includes("relationId"); + generationOptions.detached = customizations.includes("detached"); generationOptions.generateConstructor = customizations.includes( "constructor" ); diff --git a/src/models/EntityInfo.ts b/src/models/EntityInfo.ts index 50592bc..afac0f5 100644 --- a/src/models/EntityInfo.ts +++ b/src/models/EntityInfo.ts @@ -10,6 +10,7 @@ export class EntityInfo { public Indexes: IndexInfo[]; public Schema: string; public GenerateConstructor: boolean; + public detached: boolean; public IsActiveRecord: boolean; public Database: string; diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index b85b720..36b71f0 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -24,6 +24,7 @@ export function getGenerationOptions(resultsPath: string): IGenerationOptions { generateConstructor: false, customNamingStrategyPath: "", relationIds: false, + detached: false, activeRecord: false } }