Added support for detached entities, i.e. entities which are not attached to a schema and database.

This commit is contained in:
Vasil Rangelov 2019-06-24 14:59:07 +03:00
parent b70aba10c8
commit 031d74b39c
6 changed files with 18 additions and 1 deletions

View File

@ -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);
});

View File

@ -11,4 +11,5 @@ export class IGenerationOptions {
public generateConstructor: boolean = false;
public customNamingStrategyPath: string = "";
public relationIds: boolean = false;
public detached: boolean = false;
}

View File

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

View File

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

View File

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

View File

@ -24,6 +24,7 @@ export function getGenerationOptions(resultsPath: string): IGenerationOptions {
generateConstructor: false,
customNamingStrategyPath: "",
relationIds: false,
detached: false,
activeRecord: false
}
}