From 8196db67e047fb955e2ae4b30b78c224b0e7a37a Mon Sep 17 00:00:00 2001 From: Kononnable Date: Mon, 3 Jul 2017 02:36:55 +0200 Subject: [PATCH] postgres column types, PK finding; not working relations --- src/drivers/PostgresDriver.ts | 61 +++++++++++-------- src/models/ColumnInfo.ts | 26 ++++---- .../examples/sample16-indexes/entity/Post.ts | 4 +- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 6de24e4..a40271a 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -43,7 +43,7 @@ export class PostgresDriver extends AbstractDriver { let response: { table_name: string, column_name: string, column_default: string, is_nullable: string, data_type: string, character_maximum_length: number, - numeric_precision: number, numeric_scale: number, isidentity: number + numeric_precision: number, numeric_scale: number, isidentity: string }[] = (await this.Connection.query(`SELECT table_name,column_name,column_default,is_nullable, data_type,character_maximum_length,numeric_precision,numeric_scale @@ -57,7 +57,7 @@ export class PostgresDriver extends AbstractDriver { let colInfo: ColumnInfo = new ColumnInfo(); colInfo.name = resp.column_name; colInfo.is_nullable = resp.is_nullable == 'YES' ? true : false; - colInfo.is_generated = resp.isidentity == 1 ? true : false; + 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 @@ -89,26 +89,30 @@ export class PostgresDriver extends AbstractDriver { colInfo.ts_type = "boolean" colInfo.sql_type = "boolean" break; - // case "double precision": - // colInfo.ts_type = "number" - // colInfo.sql_type = "boolean" - // break; - // case "boolean": - // colInfo.ts_type = "boolean" - // colInfo.sql_type = "boolean" - // break; - // case "boolean": - // colInfo.ts_type = "boolean" - // colInfo.sql_type = "boolean" - // break; - // case "boolean": - // colInfo.ts_type = "boolean" - // colInfo.sql_type = "boolean" - // break; - // case "boolean": - // colInfo.ts_type = "boolean" - // colInfo.sql_type = "boolean" - // break; + case "double precision": + colInfo.ts_type = "number" + colInfo.sql_type = "double" + break; + case "real": + colInfo.ts_type = "number" + colInfo.sql_type = "float" + break; + case "numeric": + colInfo.ts_type = "number" + colInfo.sql_type = "decimal" + break; + case "time without time zone": + colInfo.ts_type = "Date" + colInfo.sql_type = "time" + break; + case "timestamp without time zone": + colInfo.ts_type = "Date" + colInfo.sql_type = "datetime" + break; + case "json": + colInfo.ts_type = "any" + colInfo.sql_type = "json" + break; // case "boolean": // colInfo.ts_type = "boolean" // colInfo.sql_type = "boolean" @@ -134,13 +138,13 @@ c.relname AS tablename, i.relname as indexname, f.attname AS columnname, CASE - WHEN p.contype = 'u' THEN 't' - WHEN p.contype = 'p' THEN 't' - ELSE 'f' + WHEN p.contype = 'u' THEN '1' + WHEN p.contype = 'p' THEN '1' + ELSE '0' END AS is_unique, CASE - WHEN p.contype = 'p' THEN 't' - ELSE 'f' + WHEN p.contype = 'p' THEN '1' + ELSE '0' END AS is_primary_key FROM pg_attribute f JOIN pg_class c ON c.oid = f.attrelid @@ -178,6 +182,9 @@ ORDER BY c.relname,f.attname;`)).rows; ent.Indexes.push(indexInfo); } indexColumnInfo.name = resp.columnname; + if (resp.is_primary_key==0) { + indexInfo.isPrimaryKey=false; + } // indexColumnInfo.isIncludedColumn = resp.is_included_column == 1 ? true : false; //indexColumnInfo.isDescending = resp.is_descending_key == 1 ? true : false; indexInfo.columns.push(indexColumnInfo); diff --git a/src/models/ColumnInfo.ts b/src/models/ColumnInfo.ts index 1ebdd5b..ec1308b 100644 --- a/src/models/ColumnInfo.ts +++ b/src/models/ColumnInfo.ts @@ -1,24 +1,24 @@ -import {RelationInfo} from './RelationInfo' +import { RelationInfo } from './RelationInfo' /** * ColumnInfo */ export class ColumnInfo { - name: string=''; - default: string|null=null; - is_nullable: boolean=false; - ts_type: 'number' | 'string' | 'boolean' | 'Date'; + name: string = ''; + default: string | null = null; + is_nullable: boolean = false; + ts_type: 'number' | 'string' | 'boolean' | 'Date' | 'any'; sql_type: "string" | "text" | "number" | "integer" | "int" | "smallint" | "bigint" | - "float" | "double" | "decimal" | "date" | "time" | "datetime" | "boolean" | "json" ; - char_max_lenght: number|null=null; - isPrimary:boolean=false; - is_generated:boolean=false; - numericPrecision:number|null=null; - numericScale:number|null=null; - relations:RelationInfo[]; + "float" | "double" | "decimal" | "date" | "time" | "datetime" | "boolean" | "json"; + char_max_lenght: number | null = null; + isPrimary: boolean = false; + is_generated: boolean = false; + numericPrecision: number | null = null; + numericScale: number | null = null; + relations: RelationInfo[]; constructor() { - this.relations=[]; + this.relations = []; } } diff --git a/test/integration/examples/sample16-indexes/entity/Post.ts b/test/integration/examples/sample16-indexes/entity/Post.ts index 13a775d..531fcfa 100644 --- a/test/integration/examples/sample16-indexes/entity/Post.ts +++ b/test/integration/examples/sample16-indexes/entity/Post.ts @@ -1,8 +1,8 @@ import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm" @Entity("Post") -@Index("my_index_with_id_and_text", ["id", "text"]) -@Index("my_index_with_id_and_title", (post: Post) => [post.id, post.title]) +@Index("my_index_with_id_and_text", ["id", "text"], {unique:true}) +@Index("my_index_with_id_and_title", (post: Post) => [post.id, post.title], {unique:true}) export class Post { @PrimaryGeneratedColumn()