From 7cfe6fa721d2975261a676de487a17c4c7ca9f4f Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 26 Nov 2017 21:32:17 +0100 Subject: [PATCH] support for more(all?) column types in MySQL, MariaDB --- src/drivers/MysqlDriver.ts | 74 +++++++++++---- .../entityTypes/mariadb/entity/Post.ts | 94 +++++++++++++++++++ .../entityTypes/mysql/entity/Post.ts | 93 ++++++++++++++++++ 3 files changed, 244 insertions(+), 17 deletions(-) create mode 100644 test/integration/entityTypes/mariadb/entity/Post.ts create mode 100644 test/integration/entityTypes/mysql/entity/Post.ts diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 1f5681e..75916ed 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -8,7 +8,7 @@ import { DatabaseModel } from './../models/DatabaseModel' * MysqlDriver */ export class MysqlDriver extends AbstractDriver { - readonly EngineName:string = 'MySQL' + readonly EngineName: string = 'MySQL' FindPrimaryColumnsFromIndexes(dbModel: DatabaseModel) { dbModel.entities.forEach(entity => { @@ -64,14 +64,8 @@ export class MysqlDriver extends AbstractDriver { colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH > 0 ? resp.CHARACTER_MAXIMUM_LENGTH : null; break; case "tinyint": - if (resp.NUMERIC_PRECISION == 3) { - colInfo.ts_type = "boolean" - colInfo.sql_type = "boolean" - } else { - colInfo.ts_type = "number" - colInfo.sql_type = "smallint" - colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH > 0 ? resp.CHARACTER_MAXIMUM_LENGTH : null; - } + colInfo.ts_type = "number" + colInfo.sql_type = "tinyint" break; case "smallint": colInfo.ts_type = "number" @@ -93,11 +87,11 @@ export class MysqlDriver extends AbstractDriver { colInfo.char_max_lenght = resp.CHARACTER_MAXIMUM_LENGTH > 0 ? resp.CHARACTER_MAXIMUM_LENGTH : null; break; case "date": - colInfo.ts_type = "Date" + colInfo.ts_type = "string" colInfo.sql_type = "date" break; case "time": - colInfo.ts_type = "Date" + colInfo.ts_type = "string" colInfo.sql_type = "time" break; case "datetime": @@ -120,6 +114,48 @@ export class MysqlDriver extends AbstractDriver { colInfo.ts_type = "string" colInfo.sql_type = "text" break; + + case "mediumint": + colInfo.ts_type = "number" + colInfo.sql_type = "mediumint" + break; + case "timestamp": + colInfo.ts_type = "Date" + colInfo.sql_type = "timestamp" + break; + case "year": + colInfo.ts_type = "number" + colInfo.sql_type = "year" + break; + case "blob": + colInfo.ts_type = "Buffer" + colInfo.sql_type = "blob" + break; + case "tinyblob": + colInfo.ts_type = "Buffer" + colInfo.sql_type = "tinyblob" + break; + case "tinytext": + colInfo.ts_type = "string" + colInfo.sql_type = "tinytext" + break; + case "mediumblob": + colInfo.ts_type = "Buffer" + colInfo.sql_type = "mediumblob" + break; + case "mediumtext": + colInfo.ts_type = "string" + colInfo.sql_type = "mediumtext" + break; + case "longblob": + colInfo.ts_type = "Buffer" + colInfo.sql_type = "longblob" + break; + case "longtext": + colInfo.ts_type = "string" + colInfo.sql_type = "longtext" + break; + case "varchar": colInfo.ts_type = "string" colInfo.sql_type = "varchar" @@ -155,6 +191,10 @@ export class MysqlDriver extends AbstractDriver { colInfo.ts_type = "string" colInfo.sql_type = "text" break; + case "json": + colInfo.ts_type = "Object" + colInfo.sql_type = "json" + break; default: console.error("Unknown column type:" + resp.DATA_TYPE); break; @@ -289,15 +329,15 @@ export class MysqlDriver extends AbstractDriver { isOneToMany = false; } let ownerRelation = new RelationInfo() - let columnName = ownerEntity.EntityName.toLowerCase() + (isOneToMany ? 's' : '') + let columnName = ownerEntity.EntityName.toLowerCase() + (isOneToMany ? 's' : '') if (referencedEntity.Columns.filter((filterVal) => { return filterVal.name == columnName; - }).length>0){ - for (let i=2;i<=10;i++){ - columnName = ownerEntity.EntityName.toLowerCase() + (isOneToMany ? 's' : '')+i.toString(); + }).length > 0) { + for (let i = 2; i <= 10; i++) { + columnName = ownerEntity.EntityName.toLowerCase() + (isOneToMany ? 's' : '') + i.toString(); if (referencedEntity.Columns.filter((filterVal) => { return filterVal.name == columnName; - }).length==0) break; + }).length == 0) break; } } ownerRelation.actionOnDelete = relationTmp.actionOnDelete @@ -306,7 +346,7 @@ export class MysqlDriver extends AbstractDriver { ownerRelation.relatedColumn = relatedColumn.name.toLowerCase() ownerRelation.relatedTable = relationTmp.referencedTable ownerRelation.ownerTable = relationTmp.ownerTable - ownerRelation.ownerColumn =columnName + ownerRelation.ownerColumn = columnName ownerRelation.relationType = isOneToMany ? "ManyToOne" : "OneToOne" ownerColumn.relations.push(ownerRelation) if (isOneToMany) { diff --git a/test/integration/entityTypes/mariadb/entity/Post.ts b/test/integration/entityTypes/mariadb/entity/Post.ts new file mode 100644 index 0000000..2b25f57 --- /dev/null +++ b/test/integration/entityTypes/mariadb/entity/Post.ts @@ -0,0 +1,94 @@ +import { Entity, PrimaryColumn, Column } from "typeorm"; + +@Entity("Post") +export class Post { + + @PrimaryColumn() + id: number; + + @Column() + name: string; + + @Column("int") + int: number; + + @Column("tinyint") + tinyint: number; + + @Column("smallint") + smallint: number; + + @Column("mediumint") + mediumint: number; + + @Column("bigint") + bigint: number; + + @Column("float") + float: number; + + @Column("double") + double: number; + + @Column("decimal") + decimal: number; + + @Column("date") + date: string; + + @Column("datetime") + datetime: Date; + + @Column("timestamp") + timestamp: Date; + + @Column("time") + time: string; + + @Column("year") + year: number; + + @Column("char") + char: string; + + @Column("varchar") + varchar: string; + + @Column("blob") + blob: Buffer; + + @Column("text") + text: string; + + @Column("tinyblob") + tinyblob: Buffer; + + @Column("tinytext") + tinytext: string; + + @Column("mediumblob") + mediumblob: Buffer; + + @Column("mediumtext") + mediumtext: string; + + @Column("longblob") + longblob: Buffer; + + @Column("longtext") + longtext: string; + + // @Column("enum", { enum: ["A", "B", "C"] }) + // enum: string; + + // @Column("enum", { enum: FruitEnum }) + // classEnum1: FruitEnum; + + //MariaDb type for Json - LONGTEXT + // @Column("json") + // json: Object; + + // @Column("simple-array") + // simpleArray: string[]; + +} \ No newline at end of file diff --git a/test/integration/entityTypes/mysql/entity/Post.ts b/test/integration/entityTypes/mysql/entity/Post.ts new file mode 100644 index 0000000..8149e01 --- /dev/null +++ b/test/integration/entityTypes/mysql/entity/Post.ts @@ -0,0 +1,93 @@ +import { Entity, PrimaryColumn, Column } from "typeorm"; + +@Entity("Post") +export class Post { + + @PrimaryColumn() + id: number; + + @Column() + name: string; + + @Column("int") + int: number; + + @Column("tinyint") + tinyint: number; + + @Column("smallint") + smallint: number; + + @Column("mediumint") + mediumint: number; + + @Column("bigint") + bigint: number; + + @Column("float") + float: number; + + @Column("double") + double: number; + + @Column("decimal") + decimal: number; + + @Column("date") + date: string; + + @Column("datetime") + datetime: Date; + + @Column("timestamp") + timestamp: Date; + + @Column("time") + time: string; + + @Column("year") + year: number; + + @Column("char") + char: string; + + @Column("varchar") + varchar: string; + + @Column("blob") + blob: Buffer; + + @Column("text") + text: string; + + @Column("tinyblob") + tinyblob: Buffer; + + @Column("tinytext") + tinytext: string; + + @Column("mediumblob") + mediumblob: Buffer; + + @Column("mediumtext") + mediumtext: string; + + @Column("longblob") + longblob: Buffer; + + @Column("longtext") + longtext: string; + + // @Column("enum", { enum: ["A", "B", "C"] }) + // enum: string; + + // @Column("enum", { enum: FruitEnum }) + // classEnum1: FruitEnum; + + @Column("json") + json: Object; + + // @Column("simple-array") + // simpleArray: string[]; + +} \ No newline at end of file