removing column options with default values from generated model

This commit is contained in:
Kononnable 2019-01-24 13:05:13 +01:00
parent 341632e62e
commit bbcd7fb875
8 changed files with 89 additions and 4 deletions

View File

@ -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

View File

@ -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",

View File

@ -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";

View File

@ -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";

View File

@ -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 = "";

View File

@ -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";

View File

@ -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 = "";

View File

@ -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);