changes in columnInfo model

This commit is contained in:
Kononnable 2019-01-23 01:48:45 +01:00
parent 99f3bfe3a3
commit 341632e62e
11 changed files with 114 additions and 134 deletions

View File

@ -342,10 +342,10 @@ export abstract class AbstractDriver {
primaryIndex.columns.some(
cIndex => cIndex.name === col.tsName
)
).forEach(col => (col.isPrimary = true));
).forEach(col => (col.options.primary = true));
if (
!entity.Columns.some(v => {
return v.isPrimary;
return !!v.options.primary;
})
) {
TomgUtils.LogError(

View File

@ -60,14 +60,14 @@ export class MssqlDriver extends AbstractDriver {
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.COLUMN_NAME;
colInfo.sqlName = resp.COLUMN_NAME;
colInfo.isNullable = resp.IS_NULLABLE === "YES";
colInfo.isGenerated = resp.IsIdentity === 1;
colInfo.isUnique = resp.IsUnique === 1;
colInfo.default = this.ReturnDefaultValueFunction(
colInfo.options.name = resp.COLUMN_NAME;
colInfo.options.nullable = resp.IS_NULLABLE === "YES";
colInfo.options.generated = resp.IsIdentity === 1;
colInfo.options.unique = resp.IsUnique === 1;
colInfo.options.default = this.ReturnDefaultValueFunction(
resp.COLUMN_DEFAULT
);
colInfo.sqlType = resp.DATA_TYPE;
colInfo.options.type = resp.DATA_TYPE as any;
switch (resp.DATA_TYPE) {
case "bigint":
colInfo.tsType = "string";
@ -181,24 +181,24 @@ export class MssqlDriver extends AbstractDriver {
if (
this.ColumnTypesWithPrecision.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.numericPrecision = resp.NUMERIC_PRECISION;
colInfo.numericScale = resp.NUMERIC_SCALE;
colInfo.options.precision = resp.NUMERIC_PRECISION;
colInfo.options.scale = resp.NUMERIC_SCALE;
}
if (
this.ColumnTypesWithLength.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.lenght =
colInfo.options.length =
resp.CHARACTER_MAXIMUM_LENGTH > 0
? resp.CHARACTER_MAXIMUM_LENGTH
: null;
: undefined;
}
if (colInfo.sqlType) {
if (colInfo.options.type) {
ent.Columns.push(colInfo);
}
});

View File

@ -50,21 +50,21 @@ export class MysqlDriver extends AbstractDriver {
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.COLUMN_NAME;
colInfo.sqlName = resp.COLUMN_NAME;
colInfo.isNullable = resp.IS_NULLABLE === "YES";
colInfo.isGenerated = resp.IsIdentity === 1;
colInfo.isUnique = resp.column_key === "UNI";
colInfo.default = this.ReturnDefaultValueFunction(
colInfo.options.name = resp.COLUMN_NAME;
colInfo.options.nullable = resp.IS_NULLABLE === "YES";
colInfo.options.generated = resp.IsIdentity === 1;
colInfo.options.unique = resp.column_key === "UNI";
colInfo.options.default = this.ReturnDefaultValueFunction(
resp.COLUMN_DEFAULT
);
colInfo.sqlType = resp.DATA_TYPE;
colInfo.options.type = resp.DATA_TYPE as any;
switch (resp.DATA_TYPE) {
case "int":
colInfo.tsType = "number";
break;
case "tinyint":
if (resp.column_type === "tinyint(1)") {
colInfo.width = 1;
colInfo.options.width = 1;
colInfo.tsType = "boolean";
} else {
colInfo.tsType = "number";
@ -135,7 +135,7 @@ export class MysqlDriver extends AbstractDriver {
break;
case "enum":
colInfo.tsType = "string";
colInfo.enumOptions = resp.column_type
colInfo.options.enum = resp.column_type
.substring(5, resp.column_type.length - 1)
.replace(/\'/gi, '"');
break;
@ -184,36 +184,36 @@ export class MysqlDriver extends AbstractDriver {
}
if (
this.ColumnTypesWithPrecision.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.numericPrecision = resp.NUMERIC_PRECISION;
colInfo.numericScale = resp.NUMERIC_SCALE;
colInfo.options.precision = resp.NUMERIC_PRECISION;
colInfo.options.scale = resp.NUMERIC_SCALE;
}
if (
this.ColumnTypesWithLength.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.lenght =
colInfo.options.length =
resp.CHARACTER_MAXIMUM_LENGTH > 0
? resp.CHARACTER_MAXIMUM_LENGTH
: null;
: undefined;
}
if (
this.ColumnTypesWithWidth.some(
v =>
v === colInfo.sqlType &&
v === colInfo.options.type &&
colInfo.tsType !== "boolean"
)
) {
colInfo.width =
colInfo.options.width =
resp.CHARACTER_MAXIMUM_LENGTH > 0
? resp.CHARACTER_MAXIMUM_LENGTH
: null;
: undefined;
}
if (colInfo.sqlType) {
if (colInfo.options.type) {
ent.Columns.push(colInfo);
}
});

View File

@ -62,18 +62,18 @@ export class OracleDriver extends AbstractDriver {
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.COLUMN_NAME;
colInfo.sqlName = resp.COLUMN_NAME;
colInfo.isNullable = resp.NULLABLE === "Y";
colInfo.isGenerated = resp.IDENTITY_COLUMN === "YES";
colInfo.default =
colInfo.options.name = resp.COLUMN_NAME;
colInfo.options.nullable = resp.NULLABLE === "Y";
colInfo.options.generated = resp.IDENTITY_COLUMN === "YES";
colInfo.options.default =
!resp.DATA_DEFAULT || resp.DATA_DEFAULT.includes('"')
? null
: this.ReturnDefaultValueFunction(
resp.DATA_DEFAULT
);
colInfo.isUnique = resp.IS_UNIQUE > 0;
colInfo.options.unique = resp.IS_UNIQUE > 0;
resp.DATA_TYPE = resp.DATA_TYPE.replace(/\([0-9]+\)/g, "");
colInfo.sqlType = resp.DATA_TYPE.toLowerCase();
colInfo.options.type = resp.DATA_TYPE.toLowerCase() as any;
switch (resp.DATA_TYPE.toLowerCase()) {
case "char":
colInfo.tsType = "string";
@ -170,22 +170,22 @@ export class OracleDriver extends AbstractDriver {
}
if (
this.ColumnTypesWithPrecision.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.numericPrecision = resp.DATA_PRECISION;
colInfo.numericScale = resp.DATA_SCALE;
colInfo.options.precision = resp.DATA_PRECISION;
colInfo.options.scale = resp.DATA_SCALE;
}
if (
this.ColumnTypesWithLength.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.lenght =
resp.DATA_LENGTH > 0 ? resp.DATA_LENGTH : null;
colInfo.options.length =
resp.DATA_LENGTH > 0 ? resp.DATA_LENGTH : undefined;
}
if (colInfo.sqlType) {
if (colInfo.options.type) {
ent.Columns.push(colInfo);
}
});

View File

@ -59,11 +59,11 @@ export class PostgresDriver extends AbstractDriver {
.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.column_name;
colInfo.sqlName = resp.column_name;
colInfo.isNullable = resp.is_nullable === "YES";
colInfo.isGenerated = resp.isidentity === "YES";
colInfo.isUnique = resp.isunique === "1";
colInfo.default = colInfo.isGenerated
colInfo.options.name = resp.column_name;
colInfo.options.nullable = resp.is_nullable === "YES";
colInfo.options.generated = resp.isidentity === "YES";
colInfo.options.unique = resp.isunique === "1";
colInfo.options.default = colInfo.options.generated
? null
: this.ReturnDefaultValueFunction(resp.column_default);
@ -94,10 +94,10 @@ export class PostgresDriver extends AbstractDriver {
}
return;
}
colInfo.sqlType = columnTypes.sql_type;
colInfo.options.type = columnTypes.sql_type as any;
colInfo.tsType = columnTypes.ts_type;
colInfo.isArray = columnTypes.is_array;
if (colInfo.isArray) {
colInfo.options.array = columnTypes.is_array;
if (colInfo.options.array) {
colInfo.tsType = colInfo.tsType
.split("|")
.map(x => x.replace("|", "").trim() + "[]")
@ -106,33 +106,33 @@ export class PostgresDriver extends AbstractDriver {
if (
this.ColumnTypesWithPrecision.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.numericPrecision = resp.numeric_precision;
colInfo.numericScale = resp.numeric_scale;
colInfo.options.precision = resp.numeric_precision;
colInfo.options.scale = resp.numeric_scale;
}
if (
this.ColumnTypesWithLength.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.lenght =
colInfo.options.length =
resp.character_maximum_length > 0
? resp.character_maximum_length
: null;
: undefined;
}
if (
this.ColumnTypesWithWidth.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
)
) {
colInfo.width =
colInfo.options.width =
resp.character_maximum_length > 0
? resp.character_maximum_length
: null;
: undefined;
}
if (colInfo.sqlType && colInfo.tsType) {
if (colInfo.options.type && colInfo.tsType) {
ent.Columns.push(colInfo);
}
});

View File

@ -47,22 +47,22 @@ export class SqliteDriver extends AbstractDriver {
response.forEach(resp => {
const colInfo: ColumnInfo = new ColumnInfo();
colInfo.tsName = resp.name;
colInfo.sqlName = resp.name;
colInfo.isNullable = resp.notnull === 0;
colInfo.isPrimary = resp.pk > 0;
colInfo.default = this.ReturnDefaultValueFunction(
colInfo.options.name = resp.name;
colInfo.options.nullable = resp.notnull === 0;
colInfo.options.primary = resp.pk > 0;
colInfo.options.default = this.ReturnDefaultValueFunction(
resp.dflt_value
);
colInfo.sqlType = resp.type
colInfo.options.type = resp.type
.replace(/\([0-9 ,]+\)/g, "")
.toLowerCase()
.trim();
colInfo.isGenerated =
colInfo.isPrimary &&
.trim() as any;
colInfo.options.generated =
colInfo.options.primary &&
this.tablesWithGeneratedPrimaryKey.includes(
ent.tsEntityName
);
switch (colInfo.sqlType) {
switch (colInfo.options.type) {
case "int":
colInfo.tsType = "number";
break;
@ -147,7 +147,7 @@ export class SqliteDriver extends AbstractDriver {
default:
TomgUtils.LogError(
`Unknown column type: ${
colInfo.sqlType
colInfo.options.type
} table name: ${ent.tsEntityName} column name: ${
resp.name
}`
@ -157,24 +157,24 @@ export class SqliteDriver extends AbstractDriver {
const options = resp.type.match(/\([0-9 ,]+\)/g);
if (
this.ColumnTypesWithPrecision.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
) &&
options
) {
colInfo.numericPrecision = options[0]
colInfo.options.precision = options[0]
.substring(1, options[0].length - 1)
.split(",")[0] as any;
colInfo.numericScale = options[0]
colInfo.options.scale = options[0]
.substring(1, options[0].length - 1)
.split(",")[1] as any;
}
if (
this.ColumnTypesWithLength.some(
v => v === colInfo.sqlType
v => v === colInfo.options.type
) &&
options
) {
colInfo.lenght = options[0].substring(
colInfo.options.length = options[0].substring(
1,
options[0].length - 1
) as any;
@ -182,18 +182,18 @@ export class SqliteDriver extends AbstractDriver {
if (
this.ColumnTypesWithWidth.some(
v =>
v === colInfo.sqlType &&
v === colInfo.options.type &&
colInfo.tsType !== "boolean"
) &&
options
) {
colInfo.width = options[0].substring(
colInfo.options.width = options[0].substring(
1,
options[0].length - 1
) as any;
}
if (colInfo.sqlType) {
if (colInfo.options.type) {
ent.Columns.push(colInfo);
}
});
@ -243,7 +243,7 @@ export class SqliteDriver extends AbstractDriver {
) {
ent.Columns.filter(
v => v.tsName === indexColumnInfo.name
).map(v => (v.isUnique = true));
).map(v => (v.options.unique = true));
}
indexInfo.columns.push(indexColumnInfo);
});

View File

@ -8,24 +8,24 @@ import {BaseEntity,Column,Entity,Index,JoinColumn,JoinTable,ManyToMany,ManyToOne
{{/isPrimaryKey}}{{/Indexes}}export class {{toEntityName tsEntityName}}{{#IsActiveRecord}} extends BaseEntity{{/IsActiveRecord}} {
{{#Columns}}
{{^relations}}{{#isGenerated}} @PrimaryGeneratedColumn({
type:"{{sqlType}}", {{/isGenerated}}{{^isGenerated}} @Column("{{sqlType}}",{ {{#isNullable}}
nullable:true,{{/isNullable}}{{^isNullable}}
nullable:false,{{/isNullable}}{{#isPrimary}}
primary:{{isPrimary}},{{/isPrimary}}{{/isGenerated}}{{#isUnique}}
unique: true,{{/isUnique}}{{#lenght}}
length:{{.}},{{/lenght}}{{#width}}
{{^relations}}{{#options}}{{#generated}} @PrimaryGeneratedColumn({
type:"{{type}}", {{/generated}}{{^generated}} @Column("{{type}}",{ {{#nullable}}
nullable:true,{{/nullable}}{{^nullable}}
nullable:false,{{/nullable}}{{#primary}}
primary:{{primary}},{{/primary}}{{/generated}}{{#unique}}
unique: true,{{/unique}}{{#length}}
length:{{.}},{{/length}}{{#width}}
width:{{.}},{{/width}}{{#default}}
default: {{.}},{{/default}}{{#numericPrecision}}
precision:{{.}},{{/numericPrecision}}{{#numericScale}}
scale:{{.}},{{/numericScale}}{{#enumOptions}}
enum:[{{.}}],{{/enumOptions}}{{#isArray}}
array:{{isArray}},{{/isArray}}
name:"{{sqlName}}"
})
{{printPropertyVisibility}}{{toPropertyName tsName}}:{{tsType}}{{#isNullable}} | null{{/isNullable}};
default: {{.}},{{/default}}{{#precision}}
precision:{{.}},{{/precision}}{{#scale}}
scale:{{.}},{{/scale}}{{#enum}}
enum:[{{.}}],{{/enum}}{{#array}}
array:{{array}},{{/array}}
name:"{{name}}"
}){{/options}}
{{printPropertyVisibility}}{{toPropertyName tsName}}:{{tsType}}{{#options/nullable}} | null{{/options/nullable}};
{{/relations}}{{#relations}}
@{{relationType}}(type=>{{toEntityName relatedTable}}, {{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../isPrimary}}primary:true,{{/../isPrimary}}{{^../isNullable}} nullable:false,{{/../isNullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }}{{#actionOnDelete}},{ onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}}
@{{relationType}}(type=>{{toEntityName relatedTable}}, {{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{#if isOwner}}{{toPropertyName ownerColumn}},{ {{#../options/primary}}primary:true,{{/../options/primary}}{{^../options/nullable}} nullable:false,{{/../options/nullable}}{{#actionOnDelete}}onDelete: '{{.}}',{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{else}}{{toPropertyName relatedColumn}}{{#if (or actionOnDelete actionOnUpdate ) }}{{#actionOnDelete}},{ onDelete: '{{.}}' ,{{/actionOnDelete}}{{#actionOnUpdate}}onUpdate: '{{.}}'{{/actionOnUpdate}} }{{/if}}{{/if}}){{#isOwner}}
{{#if isManyToMany}}@JoinTable(){{else}}@JoinColumn({ name:'{{ ../sqlName}}'}){{/if}}{{/isOwner}}
{{#if (or isOneToMany isManyToMany)}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) "[]")}};
{{else}}{{printPropertyVisibility}}{{toPropertyName ../tsName}}:{{toLazy (concat (toEntityName relatedTable) ' | null')}};

View File

@ -1,11 +1,9 @@
import { ColumnOptions } from "typeorm";
import { RelationInfo } from "./RelationInfo";
export class ColumnInfo {
public options: ColumnOptions = {};
public tsName: string = "";
public sqlName: string = "";
public default: string | null = null;
public isNullable: boolean = false;
public isUnique: boolean = false;
public tsType:
| "number"
| "string"
@ -16,17 +14,5 @@ export class ColumnInfo {
| "string | Object"
| "string | string[]"
| "any";
public sqlType: string;
public lenght: number | null = null;
public width: number | null = null;
public isPrimary: boolean = false;
public isGenerated: boolean = false;
public isArray: boolean = false;
public numericPrecision: number | null = null;
public numericScale: number | null = null;
public enumOptions: string | null = null;
public relations: RelationInfo[];
constructor() {
this.relations = [];
}
public relations: RelationInfo[] = [];
}

View File

@ -81,21 +81,16 @@ describe('MssqlDriver', function () {
entities.push(y)
const expected: EntityInfo[] = JSON.parse(JSON.stringify(entities));
expected[0].Columns.push({
lenght: null,
default: `() => "'a'"`,
isNullable: true,
isPrimary: false,
isGenerated: true,
options: {
default: `() => "'a'"`,
nullable: true,
generated: true,
name: 'name',
unique:false,
type: 'int',
},
tsName: 'name',
sqlName: 'name',
numericPrecision: null,
numericScale: null,
width: null,
sqlType: 'int',
tsType: 'number',
enumOptions: null,
isUnique:false,
isArray:false,
relations: [] as RelationInfo[],
})
const result = await driver.GetCoulmnsFromEntity(entities, 'schema');

View File

@ -25,14 +25,14 @@ describe("Platform specyfic types", async function () {
runTestsFromPath(testPartialPath, true);
})
describe("GitHub issues", async function () {
this.timeout(30000)
this.slow(5000)// compiling created models takes time
this.timeout(60000)
this.slow(30000)// compiling created models takes time
const testPartialPath = 'test/integration/github-issues'
runTestsFromPath(testPartialPath, false);
})
describe("TypeOrm examples", async function () {
this.timeout(30000)
this.slow(5000)// compiling created models takes time
this.timeout(60000)
this.slow(30000)// compiling created models takes time
const testPartialPath = 'test/integration/examples'
runTestsFromPath(testPartialPath, false);
})

View File

@ -1,4 +1,3 @@
export class EntityFileToJson {
public getEntityOptions(trimmedLine: string, ent: EntityJson) {
const decoratorParameters = trimmedLine.slice(trimmedLine.indexOf('(') + 1, trimmedLine.lastIndexOf(')'))