From beaf3a09da96ed1bca2c945b3ad1c352a48f7af0 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 10 Nov 2019 14:54:48 +0100 Subject: [PATCH] fix removal of some of columns from relations proper referencedColumnName value --- src/ModelCustomization.ts | 5 ++- src/drivers/AbstractDriver.ts | 4 +-- src/drivers/MssqlDriver.ts | 31 +++++++++---------- src/drivers/MysqlDriver.ts | 3 +- src/drivers/OracleDriver.ts | 3 +- src/drivers/PostgresDriver.ts | 3 +- src/drivers/SqliteDriver.ts | 3 +- src/models/Column.ts | 3 +- src/templates/entity.mst | 2 +- test/drivers/MssqlDriver.test.ts | 3 +- .../modelCustomization.test.ts | 15 +++------ 11 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/ModelCustomization.ts b/src/ModelCustomization.ts index 8c12c82..d635bb3 100644 --- a/src/ModelCustomization.ts +++ b/src/ModelCustomization.ts @@ -30,7 +30,10 @@ export default function modelCustomizationPhase( function removeColumnsInRelation(dbModel: Entity[]): Entity[] { dbModel.forEach(entity => { entity.columns = entity.columns.filter( - col => !col.isUsedInRelation || col.primary + col => + !col.isUsedInRelationAsOwner || + col.isUsedInRelationAsReferenced || + col.primary ); }); return dbModel; diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 47dfe9e..cbfe754 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -318,10 +318,10 @@ export default abstract class AbstractDriver { isOneToMany = !index; ownerColumns.forEach(column => { - column.isUsedInRelation = true; + column.isUsedInRelationAsOwner = true; }); relatedColumns.forEach(column => { - column.isUsedInRelation = true; + column.isUsedInRelationAsReferenced = true; }); let fieldName = ""; if (ownerColumns.length === 1) { diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 4adf75e..92efbc3 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -57,22 +57,22 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG IsIdentity: number; IsUnique: number; }[] = (await request.query(`SELECT TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE, - DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE, - COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') IsIdentity, - (SELECT count(*) - FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc - inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu - on cu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME - where - tc.CONSTRAINT_TYPE = 'UNIQUE' - and tc.TABLE_NAME = c.TABLE_NAME - and cu.COLUMN_NAME = c.COLUMN_NAME - and tc.TABLE_SCHEMA=c.TABLE_SCHEMA) IsUnique - FROM INFORMATION_SCHEMA.COLUMNS c - where TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList( + DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE, + COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') IsIdentity, + (SELECT count(*) + FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc + inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu + on cu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME + where + tc.CONSTRAINT_TYPE = 'UNIQUE' + and tc.TABLE_NAME = c.TABLE_NAME + and cu.COLUMN_NAME = c.COLUMN_NAME + and tc.TABLE_SCHEMA=c.TABLE_SCHEMA) IsUnique + FROM INFORMATION_SCHEMA.COLUMNS c + where TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList( dbNames )}) - order by ordinal_position`)).recordset; + order by ordinal_position`)).recordset; entities.forEach(ent => { response .filter(filterVal => { @@ -226,8 +226,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG default: defaultValue, options, tscName, - tscType, - isUsedInRelation: false + tscType }); } }); diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 44b5acd..75aab4a 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -257,8 +257,7 @@ export default class MysqlDriver extends AbstractDriver { default: defaultValue, options, tscName, - tscType, - isUsedInRelation: false + tscType }); } }); diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index 16b255c..38cf79b 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -208,8 +208,7 @@ export default class OracleDriver extends AbstractDriver { default: defaultValue, options, tscName, - tscType, - isUsedInRelation: false + tscType }); } }); diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 0aae984..c34402f 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -159,8 +159,7 @@ export default class PostgresDriver extends AbstractDriver { default: defaultValue, options, tscName, - tscType, - isUsedInRelation: false + tscType }); } }); diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index 1d07aba..64ce684 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -226,8 +226,7 @@ export default class SqliteDriver extends AbstractDriver { default: defaultValue, options, tscName, - tscType, - isUsedInRelation: false + tscType }); } }); diff --git a/src/models/Column.ts b/src/models/Column.ts index b14fd41..f62271e 100644 --- a/src/models/Column.ts +++ b/src/models/Column.ts @@ -4,7 +4,8 @@ export type Column = { tscType: string; tscName: string; type: ColumnType | string; // TODO: remove ? - isUsedInRelation: boolean; // TODO: move to separate object/calulate when us + isUsedInRelationAsOwner?: true; // TODO: move to separate object/calulate when us + isUsedInRelationAsReferenced?: true; // TODO: move to separate object/calulate when us primary?: boolean; generated?: true | "increment" | "uuid"; diff --git a/src/templates/entity.mst b/src/templates/entity.mst index 17f714b..8cfa03c 100644 --- a/src/templates/entity.mst +++ b/src/templates/entity.mst @@ -11,7 +11,7 @@ import { {{toEntityName .}} } from './{{toFileName .}}' {{/inline}} {{#*inline "Relation"}} @{{relationType}}(()=>{{toEntityName relatedTable}},{{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{toPropertyName relatedField}}{{#if relationOptions}},{ {{json relationOptions}} }{{/if}}) -{{#if joinColumnOptions}}@JoinColumn([{{json joinColumnOptions}}]){{/if}} +{{#if joinColumnOptions}}@JoinColumn([{{#joinColumnOptions}}{ name: "{{name}}", referencedColumnName: "{{toPropertyName referencedColumnName}}" },{{/joinColumnOptions}}]){{/if}} {{#if joinTableOptions}}@JoinTable({ {{json joinTableOptions}} }){{/if}} {{printPropertyVisibility}}{{toPropertyName fieldName}}{{strictMode}}:{{toRelation (toEntityName relatedTable) relationType}}; diff --git a/test/drivers/MssqlDriver.test.ts b/test/drivers/MssqlDriver.test.ts index c7a5a2d..bfa577f 100644 --- a/test/drivers/MssqlDriver.test.ts +++ b/test/drivers/MssqlDriver.test.ts @@ -106,8 +106,7 @@ describe("MssqlDriver", () => { generated: true, default: `() => "'a'"`, tscName: "name", - tscType: "number", - isUsedInRelation: false + tscType: "number" }); const result = await driver.GetCoulmnsFromEntity( diff --git a/test/modelCustomization/modelCustomization.test.ts b/test/modelCustomization/modelCustomization.test.ts index 207f20c..5ba0ff1 100644 --- a/test/modelCustomization/modelCustomization.test.ts +++ b/test/modelCustomization/modelCustomization.test.ts @@ -21,15 +21,13 @@ describe("Model customization phase", async () => { options: { name: "id" }, tscName: "id", tscType: "number", - primary: true, - isUsedInRelation: false + primary: true }, { type: "character varying", options: { name: "name" }, tscName: "name", - tscType: "string", - isUsedInRelation: false + tscType: "string" } ], indices: [ @@ -63,22 +61,19 @@ describe("Model customization phase", async () => { options: { name: "id" }, tscName: "id", tscType: "number", - primary: true, - isUsedInRelation: false + primary: true }, { type: "character varying", options: { name: "title" }, tscName: "title", - tscType: "string", - isUsedInRelation: false + tscType: "string" }, { type: "character varying", options: { name: "text" }, tscName: "text", - tscType: "string", - isUsedInRelation: false + tscType: "string" } ], indices: [