diff --git a/src/Engine.ts b/src/Engine.ts index 3bf307a..d8210ad 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -2,6 +2,7 @@ import changeCase = require("change-case"); import fs = require("fs"); import * as Handlebars from "handlebars"; import path = require("path"); +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { AbstractNamingStrategy } from "./AbstractNamingStrategy"; import { AbstractDriver } from "./drivers/AbstractDriver"; import { MariaDbDriver } from "./drivers/MariaDbDriver"; @@ -47,7 +48,11 @@ export async function createModelFromDatabase( ); return; } - dbModel = modelCustomizationPhase(dbModel, generationOptions); + dbModel = modelCustomizationPhase( + dbModel, + generationOptions, + driver.defaultValues + ); modelGenerationPhase(connectionOptions, generationOptions, dbModel); } export async function dataCollectionPhase( @@ -59,14 +64,63 @@ export async function dataCollectionPhase( export function modelCustomizationPhase( dbModel: EntityInfo[], - generationOptions: IGenerationOptions + generationOptions: IGenerationOptions, + defaultValues: DataTypeDefaults ) { dbModel = setRelationId(generationOptions, dbModel); dbModel = applyNamingStrategy(generationOptions.namingStrategy, dbModel); dbModel = addImportsAndGenerationOptions(dbModel, generationOptions); + dbModel = removeColumnDefaultProperties(dbModel, defaultValues); + return dbModel; +} +function removeColumnDefaultProperties( + dbModel: EntityInfo[], + defaultValues: DataTypeDefaults +) { + if (!defaultValues) { + return dbModel; + } + dbModel.forEach(entity => { + entity.Columns.forEach(column => { + const defVal = defaultValues[column.options.type as any]; + if (defVal) { + if ( + column.options.length && + defVal.length && + column.options.length === defVal.length + ) { + column.options.length = undefined; + // console.log(`Default length for ${column.options.type}`) + } + if ( + column.options.precision && + defVal.precision && + column.options.precision === defVal.precision + ) { + column.options.precision = undefined; + // console.log(`Default precision for ${column.options.type}`) + } + if ( + column.options.scale && + defVal.scale && + column.options.scale === defVal.scale + ) { + column.options.scale = undefined; + // console.log(`Default scale for ${column.options.type}`) + } + if ( + column.options.width && + defVal.width && + column.options.width === defVal.width + ) { + column.options.width = undefined; + // console.log(`Default width for ${column.options.type}`) + } + } + }); + }); return dbModel; } - function addImportsAndGenerationOptions( dbModel: EntityInfo[], generationOptions: IGenerationOptions diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index d1eabad..b1eb066 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -3,6 +3,7 @@ import { WithPrecisionColumnType, WithWidthColumnType } from "typeorm/driver/types/ColumnTypes"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -13,6 +14,7 @@ export abstract class AbstractDriver { public abstract standardPort: number; public abstract standardSchema: string; public abstract standardUser: string; + public abstract defaultValues: DataTypeDefaults; public ColumnTypesWithWidth: WithWidthColumnType[] = [ "tinyint", diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index de5eeb9..67077d0 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -1,4 +1,7 @@ import * as MSSQL from "mssql"; +import { ConnectionOptions } from "typeorm"; +import * as TypeormDriver from "typeorm/driver/sqlserver/SqlServerDriver"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -6,6 +9,9 @@ import * as TomgUtils from "../Utils"; import { AbstractDriver } from "./AbstractDriver"; export class MssqlDriver extends AbstractDriver { + public defaultValues: DataTypeDefaults = new TypeormDriver.SqlServerDriver({ + options: { replication: undefined } as ConnectionOptions + } as any).dataTypeDefaults; public readonly standardPort = 1433; public readonly standardSchema = "dbo"; public readonly standardUser = "sa"; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 566739a..34cf412 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -1,4 +1,7 @@ import * as MYSQL from "mysql"; +import { ConnectionOptions } from "typeorm"; +import * as TypeormDriver from "typeorm/driver/mysql/MysqlDriver"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -6,6 +9,9 @@ import * as TomgUtils from "../Utils"; import { AbstractDriver } from "./AbstractDriver"; export class MysqlDriver extends AbstractDriver { + public defaultValues: DataTypeDefaults = new TypeormDriver.MysqlDriver({ + options: { replication: undefined } as ConnectionOptions + } as any).dataTypeDefaults; public readonly EngineName: string = "MySQL"; public readonly standardPort = 3306; public readonly standardUser = "root"; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index fa973f6..4eafdec 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -1,3 +1,5 @@ +import * as TypeormDriver from "typeorm/driver/oracle/OracleDriver"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -5,6 +7,9 @@ import * as TomgUtils from "../Utils"; import { AbstractDriver } from "./AbstractDriver"; export class OracleDriver extends AbstractDriver { + public defaultValues: DataTypeDefaults = new TypeormDriver.OracleDriver({ + options: undefined + } as any).dataTypeDefaults; public readonly standardPort = 1521; public readonly standardUser = "SYS"; public readonly standardSchema = ""; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 2890166..e1911a1 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -1,4 +1,7 @@ import * as PG from "pg"; +import { ConnectionOptions } from "typeorm"; +import * as TypeormDriver from "typeorm/driver/postgres/PostgresDriver"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -6,6 +9,9 @@ import * as TomgUtils from "../Utils"; import { AbstractDriver } from "./AbstractDriver"; export class PostgresDriver extends AbstractDriver { + public defaultValues: DataTypeDefaults = new TypeormDriver.PostgresDriver({ + options: { replication: undefined } as ConnectionOptions + } as any).dataTypeDefaults; public readonly standardPort = 5432; public readonly standardUser = "postgres"; public readonly standardSchema = "public"; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index 4b548aa..9f492f6 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -1,3 +1,6 @@ +import { ConnectionOptions } from "typeorm"; +import * as TypeormDriver from "typeorm/driver/sqlite/SqliteDriver"; +import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; import { IConnectionOptions } from "../Engine"; import { ColumnInfo } from "../models/ColumnInfo"; import { EntityInfo } from "../models/EntityInfo"; @@ -5,6 +8,9 @@ import * as TomgUtils from "../Utils"; import { AbstractDriver } from "./AbstractDriver"; export class SqliteDriver extends AbstractDriver { + public defaultValues: DataTypeDefaults = new TypeormDriver.SqliteDriver({ + options: { database: "true" } as ConnectionOptions + } as any).dataTypeDefaults; public readonly standardPort = 0; public readonly standardUser = ""; public readonly standardSchema = ""; diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index c27c66e..ade1506 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -70,7 +70,7 @@ function runTestForMultipleDrivers(testName: string, dbDrivers: string[], testPa const modelGenerationPromises = driversToRun.map(async (dbDriver) => { const { generationOptions, driver, connectionOptions, resultsPath, filesOrgPathTS } = await prepareTestRuns(testPartialPath, testName, dbDriver); let dbModel = await dataCollectionPhase(driver, connectionOptions); - dbModel = modelCustomizationPhase(dbModel, generationOptions); + dbModel = modelCustomizationPhase(dbModel, generationOptions, driver.defaultValues); const filesGenPath = path.resolve(resultsPath, 'entities'); modelGenerationPhase(connectionOptions, generationOptions, dbModel); compareGeneratedFiles(filesOrgPathTS, filesGenPath);