From 341632e62e2fb937eea359cc6049335bdf7e16f2 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 23 Jan 2019 01:48:45 +0100 Subject: [PATCH] changes in columnInfo model --- src/drivers/AbstractDriver.ts | 4 +-- src/drivers/MssqlDriver.ts | 26 ++++++++-------- src/drivers/MysqlDriver.ts | 36 ++++++++++----------- src/drivers/OracleDriver.ts | 26 ++++++++-------- src/drivers/PostgresDriver.ts | 36 ++++++++++----------- src/drivers/SqliteDriver.ts | 38 +++++++++++------------ src/entity.mst | 32 +++++++++---------- src/models/ColumnInfo.ts | 20 ++---------- test/drivers/MssqlDriver.test.ts | 21 +++++-------- test/integration/runTestsFromPath.test.ts | 8 ++--- test/utils/EntityFileToJson.ts | 1 - 11 files changed, 114 insertions(+), 134 deletions(-) diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 882447b..d1eabad 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -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( diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 7ab2a6a..de5eeb9 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -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); } }); diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index bccbd65..566739a 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -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); } }); diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index 4ccf0e9..fa973f6 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -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); } }); diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index ebe741c..2890166 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -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); } }); diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index b65ba2b..4b548aa 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -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); }); diff --git a/src/entity.mst b/src/entity.mst index eb6846f..99c3ae5 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -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')}}; diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index 8cdaea3..24307c6 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -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[] = []; } diff --git a/test/drivers/MssqlDriver.test.ts b/test/drivers/MssqlDriver.test.ts index 50dfe69..c2cc4a2 100644 --- a/test/drivers/MssqlDriver.test.ts +++ b/test/drivers/MssqlDriver.test.ts @@ -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'); diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index b3ae9d0..c27c66e 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -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); }) diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts index 300e56c..237ec8f 100644 --- a/test/utils/EntityFileToJson.ts +++ b/test/utils/EntityFileToJson.ts @@ -1,4 +1,3 @@ - export class EntityFileToJson { public getEntityOptions(trimmedLine: string, ent: EntityJson) { const decoratorParameters = trimmedLine.slice(trimmedLine.indexOf('(') + 1, trimmedLine.lastIndexOf(')'))