From 4e5393d00eba7dacf43a685267bc4d7c919704dc Mon Sep 17 00:00:00 2001 From: Kononnable Date: Mon, 20 Nov 2017 22:23:00 +0100 Subject: [PATCH] support for more(all?) column types in Postgres --- src/drivers/PostgresDriver.ts | 120 +++++++-- src/models/ColumnInfo.ts | 2 +- .../entityTypes/postgres/entity/Post.ts | 240 ++++++++++++++++++ .../entity/EverythingEntity.ts | 12 +- 4 files changed, 341 insertions(+), 33 deletions(-) create mode 100644 test/integration/entityTypes/postgres/entity/Post.ts diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 7e9f0c4..2348b32 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -23,7 +23,7 @@ export class PostgresDriver extends AbstractDriver { }); } - async GetAllTables(schema:string): Promise { + async GetAllTables(schema: string): Promise { let response: { table_schema: string, table_name: string }[] = (await this.Connection.query(`SELECT table_schema,table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND table_schema = '${schema}' `)).rows; @@ -38,7 +38,7 @@ export class PostgresDriver extends AbstractDriver { }) return ret; } - async GetCoulmnsFromEntity(entities: EntityInfo[],schema:string): Promise { + async GetCoulmnsFromEntity(entities: EntityInfo[], schema: string): Promise { let response: { table_name: string, column_name: string, column_default: string, is_nullable: string, data_type: string, character_maximum_length: number, @@ -59,16 +59,14 @@ export class PostgresDriver extends AbstractDriver { colInfo.is_generated = resp.isidentity == 'YES' ? true : false; colInfo.default = colInfo.is_generated ? '' : resp.column_default; switch (resp.data_type) { - //TODO:change types to postgres case "integer": colInfo.ts_type = "number" colInfo.sql_type = "int" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; break; case "character varying": colInfo.ts_type = "string" colInfo.sql_type = "character varying" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; + colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length : null; break; case "text": colInfo.ts_type = "string" @@ -81,15 +79,13 @@ export class PostgresDriver extends AbstractDriver { case "smallint": colInfo.ts_type = "number" colInfo.sql_type = "smallint" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; break; case "bigint": - colInfo.ts_type = "number" + colInfo.ts_type = "string" colInfo.sql_type = "bigint" - 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 "boolean": @@ -99,21 +95,24 @@ export class PostgresDriver extends AbstractDriver { case "double precision": colInfo.ts_type = "number" colInfo.sql_type = "double" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; + colInfo.numericPrecision = resp.numeric_precision + colInfo.numericScale = resp.numeric_scale break; case "real": colInfo.ts_type = "number" colInfo.sql_type = "float" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; + colInfo.numericPrecision = resp.numeric_precision + colInfo.numericScale = resp.numeric_scale break; case "numeric": - colInfo.ts_type = "number" - colInfo.sql_type = "decimal" - colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length: null; + colInfo.ts_type = "string" + colInfo.sql_type = "numeric" + colInfo.numericPrecision = resp.numeric_precision + colInfo.numericScale = resp.numeric_scale break; case "time without time zone": - colInfo.ts_type = "Date" - colInfo.sql_type = "time" + colInfo.ts_type = "string" + colInfo.sql_type = "time without time zone" break; case "timestamp without time zone": colInfo.ts_type = "Date" @@ -124,17 +123,86 @@ export class PostgresDriver extends AbstractDriver { colInfo.sql_type = "timestamp" break; case "json": - colInfo.ts_type = "any" + colInfo.ts_type = "Object" colInfo.sql_type = "json" break; case "jsonb": - colInfo.ts_type = "any" + colInfo.ts_type = "Object" colInfo.sql_type = "jsonb" break; - // case "boolean": - // colInfo.ts_type = "boolean" - // colInfo.sql_type = "boolean" - // break; + case "money": + colInfo.ts_type = "string" + colInfo.sql_type = "money" + break; + case "character": + colInfo.ts_type = "string" + colInfo.sql_type = "character" + colInfo.char_max_lenght = resp.character_maximum_length > 0 ? resp.character_maximum_length : null; + break; + case "bytea": + colInfo.ts_type = "Buffer" + colInfo.sql_type = "bytea" + break; + case "interval": + colInfo.ts_type = "any" + colInfo.sql_type = "interval" + break; + case "time with time zone": + colInfo.ts_type = "string" + colInfo.sql_type = "time with time zone" + break; + case "point": + colInfo.ts_type = "string|Object" + colInfo.sql_type = "point" + break; + case "line": + colInfo.ts_type = "string" + colInfo.sql_type = "line" + break; + case "lseg": + colInfo.ts_type = "string|string[]" + colInfo.sql_type = "lseg" + break; + case "box": + colInfo.ts_type = "string|Object" + colInfo.sql_type = "box" + break; + case "path": + colInfo.ts_type = "string" + colInfo.sql_type = "path" + break; + case "polygon": + colInfo.ts_type = "string" + colInfo.sql_type = "polygon" + break; + case "circle": + colInfo.ts_type = "string|Object" + colInfo.sql_type = "circle" + break; + case "cidr": + colInfo.ts_type = "string" + colInfo.sql_type = "cidr" + break; + case "inet": + colInfo.ts_type = "string" + colInfo.sql_type = "inet" + break; + case "macaddr": + colInfo.ts_type = "string" + colInfo.sql_type = "macaddr" + break; + case "bit": + colInfo.ts_type = "string" + colInfo.sql_type = "bit" + break; + case "bit varying": + colInfo.ts_type = "string" + colInfo.sql_type = "bit varying" + break; + case "xml": + colInfo.ts_type = "string" + colInfo.sql_type = "xml" + break; default: console.error("Unknown column type:" + resp.data_type); @@ -146,7 +214,7 @@ export class PostgresDriver extends AbstractDriver { }) return entities; } - async GetIndexesFromEntity(entities: EntityInfo[],schema:string): Promise { + async GetIndexesFromEntity(entities: EntityInfo[], schema: string): Promise { let response: { tablename: string, indexname: string, columnname: string, is_unique: number, is_primary_key: number//, is_descending_key: number//, is_included_column: number @@ -211,7 +279,7 @@ export class PostgresDriver extends AbstractDriver { return entities; } - async GetRelations(entities: EntityInfo[],schema:string): Promise { + async GetRelations(entities: EntityInfo[], schema: string): Promise { let response: { tablewithforeignkey: string, fk_partno: number, foreignkeycolumn: string, tablereferenced: string, foreignkeycolumnreferenced: string, @@ -383,14 +451,14 @@ export class PostgresDriver extends AbstractDriver { } } - async ConnectToServer(database: string, server: string, port: number, user: string, password: string,ssl:boolean) { + async ConnectToServer(database: string, server: string, port: number, user: string, password: string, ssl: boolean) { this.Connection = new PG.Client({ database: database, host: server, port: port, user: user, password: password, - ssl:ssl + ssl: ssl }) diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index be07811..fa1efcc 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -7,7 +7,7 @@ export class ColumnInfo { name: string = ''; default: string | null = null; is_nullable: boolean = false; - ts_type: 'number' | 'string' | 'boolean' | 'Date' | 'Buffer' | 'any'; + ts_type: 'number' | 'string' | 'boolean' | 'Date' | 'Buffer' | 'Object' | 'string|Object' | 'string|string[]' | 'any'; sql_type: ColumnType; char_max_lenght: number | null = null; isPrimary: boolean = false; diff --git a/test/integration/entityTypes/postgres/entity/Post.ts b/test/integration/entityTypes/postgres/entity/Post.ts new file mode 100644 index 0000000..9acf3ea --- /dev/null +++ b/test/integration/entityTypes/postgres/entity/Post.ts @@ -0,0 +1,240 @@ +import { Entity, PrimaryColumn, Column } from "typeorm"; + +@Entity("Post") +export class Post { + + @PrimaryColumn() + id: number; + + @Column() + name: string; + + // ------------------------------------------------------------------------- + // Numeric Types + // ------------------------------------------------------------------------- + + @Column("integer") + integer: number; + + @Column("int4") + int4: number; + + @Column("int") + int: number; + + @Column("smallint") + smallint: number; + + @Column("int2") + int2: number; + + @Column("bigint") + bigint: string; + + @Column("int8") + int8: string; + + // @Column("serial") + // serial: number; + + // @Column("serial4") + // serial4: number; + + // @Column("smallserial") + // smallserial: number; + + // @Column("serial2") + // serial2: number; + + // @Column("bigserial") + // bigserial: number; + + // @Column("serial8") + // serial8: number; + + @Column("numeric") + numeric: string; + + @Column("decimal") + decimal: string; + + @Column("double precision") + doublePrecision: number; + + @Column("float8") + float8: number; + + @Column("real") + real: number; + + @Column("float4") + float4: number; + + // ------------------------------------------------------------------------- + // Monetary Types + // ------------------------------------------------------------------------- + + @Column("money") + money: string; + + // ------------------------------------------------------------------------- + // Character Types + // ------------------------------------------------------------------------- + + @Column("char") + char: string; + + @Column("character") + character: string; + + @Column("varchar") + varchar: string; + + @Column("character varying") + characterVarying: string; + + @Column("text") + text: string; + + // @Column("citext") + // citext: string; + + // ------------------------------------------------------------------------- + // Binary Data Types + // ------------------------------------------------------------------------- + + @Column("bytea") + bytea: Buffer; + + // ------------------------------------------------------------------------- + // Date/Time Types + // ------------------------------------------------------------------------- + + @Column("date") + date: string; + + @Column("interval") + interval: any; + + @Column("time") + time: string; + + @Column("time with time zone") + timeWithTimeZone: string; + + @Column("timetz") + timetz: string; + + @Column("timestamp") + timestamp: Date; + + @Column("timestamp with time zone") + timestampWithTimeZone: Date; + + @Column("timestamptz") + timestamptz: Date; + + // ------------------------------------------------------------------------- + // Boolean Type + // ------------------------------------------------------------------------- + + @Column("boolean") + boolean: boolean; + + @Column("bool") + bool: boolean; + + // ------------------------------------------------------------------------- + // Enumerated Type + // ------------------------------------------------------------------------- + + // @Column("enum", { enum: ["A", "B", "C"] }) + // enum: string; + + // ------------------------------------------------------------------------- + // Geometric Type + // ------------------------------------------------------------------------- + + @Column("point") + point: string | Object; + + @Column("line") + line: string; + + @Column("lseg") + lseg: string | string[]; + + @Column("box") + box: string | Object; + + @Column("path") + path: string; + + @Column("polygon") + polygon: string; + + @Column("circle") + circle: string | Object; + + // ------------------------------------------------------------------------- + // Network Address Type + // ------------------------------------------------------------------------- + + @Column("cidr") + cidr: string; + + @Column("inet") + inet: string; + + @Column("macaddr") + macaddr: string; + + // ------------------------------------------------------------------------- + // Bit String Type + // ------------------------------------------------------------------------- + + @Column("bit") + bit: string; + + @Column("varbit") + varbit: string; + + @Column("bit varying") + bitVarying: string; + + // ------------------------------------------------------------------------- + // UUID Type + // ------------------------------------------------------------------------- + + @Column("uuid") + uuid: string; + + // ------------------------------------------------------------------------- + // XML Type + // ------------------------------------------------------------------------- + + @Column("xml") + xml: string; + + // ------------------------------------------------------------------------- + // JSON Type + // ------------------------------------------------------------------------- + + @Column("json") + json: Object; + + // ------------------------------------------------------------------------- + // Array Type + // ------------------------------------------------------------------------- + + // @Column("int", { isArray: true }) + // array: number[]; + + // // ------------------------------------------------------------------------- + // // TypeOrm Specific Type + // // ------------------------------------------------------------------------- + + // @Column("simple-array") + // simpleArray: string[]; + +} \ No newline at end of file diff --git a/test/integration/examples/sample11-all-types-entity/entity/EverythingEntity.ts b/test/integration/examples/sample11-all-types-entity/entity/EverythingEntity.ts index 023040c..a2eb712 100644 --- a/test/integration/examples/sample11-all-types-entity/entity/EverythingEntity.ts +++ b/test/integration/examples/sample11-all-types-entity/entity/EverythingEntity.ts @@ -36,17 +36,17 @@ export class EverythingEntity { // @Column("double") // doubleColumn: number; - @Column("decimal") - decimalColumn: number; + // @Column("decimal") + // decimalColumn: number; @Column() date: Date; - @Column("date") - dateColumn: Date; + // @Column("date") + // dateColumn: Date; - @Column("time") - timeColumn: Date; + // @Column("time") + // timeColumn: Date; // @Column("boolean") // isBooleanColumn: boolean;