code changes, test for nullable columns

This commit is contained in:
Kononnable 2019-12-29 22:54:30 +01:00
parent 3bbabd127b
commit f0a6f67429
8 changed files with 75 additions and 61 deletions

View File

@ -376,19 +376,20 @@ export default abstract class AbstractDriver {
ownerColumns[0].tscName,
ownerEntity
);
let relationIdType = ownerColumns[0].tscType;
if (ownerColumns[0].options.nullable) {
relationIdType += " | null";
let fieldType = "";
if (isOneToMany) {
fieldType = `${ownerColumns[0].tscType}[]`;
} else {
fieldType = ownerColumns[0].tscType;
if (ownerColumns[0].options.nullable) {
fieldType += " | null";
}
}
ownerEntity.relationIds.push({
fieldName: relationIdFieldName,
fieldType: isOneToMany
? `${
relationIdType.includes(" ")
? `(${relationIdType})`
: relationIdType
}[]`
: relationIdType,
fieldType,
relationField: ownerRelation.fieldName
});
// TODO: RelationId on ManyToMany

View File

@ -90,7 +90,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
resp.COLUMN_DEFAULT
);
const columnType = resp.DATA_TYPE;
let tscType = "NonNullable<unknown>";
let tscType = "";
switch (resp.DATA_TYPE) {
case "bigint":
tscType = "string";
@ -192,6 +192,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
tscType = "string";
break;
default:
tscType = "NonNullable<unknown>";
TomgUtils.LogError(
`Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}`
);
@ -218,17 +219,14 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
? resp.CHARACTER_MAXIMUM_LENGTH
: undefined;
}
if (columnType) {
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
}
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
});
});
return entities;

View File

@ -69,7 +69,7 @@ export default class MysqlDriver extends AbstractDriver {
.filter(filterVal => filterVal.TABLE_NAME === ent.tscName)
.forEach(resp => {
const tscName = resp.COLUMN_NAME;
let tscType = "NonNullable<unknown>";
let tscType = "";
const options: Column["options"] = {
name: resp.COLUMN_NAME
};
@ -214,6 +214,7 @@ export default class MysqlDriver extends AbstractDriver {
tscType = "string";
break;
default:
tscType = "NonNullable<unknown>";
TomgUtils.LogError(
`Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}`
);
@ -250,16 +251,14 @@ export default class MysqlDriver extends AbstractDriver {
: undefined;
}
if (columnType) {
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
}
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
});
});
return entities;

View File

@ -87,7 +87,7 @@ export default class OracleDriver extends AbstractDriver {
);
const DATA_TYPE = resp.DATA_TYPE.replace(/\([0-9]+\)/g, "");
const columnType = DATA_TYPE.toLowerCase();
let tscType = "NonNullable<unknown>";
let tscType = "";
switch (DATA_TYPE.toLowerCase()) {
case "char":
tscType = "string";
@ -177,6 +177,7 @@ export default class OracleDriver extends AbstractDriver {
tscType = "number";
break;
default:
tscType = "NonNullable<unknown>";
TomgUtils.LogError(
`Unknown column type:${DATA_TYPE}`
);
@ -201,16 +202,14 @@ export default class OracleDriver extends AbstractDriver {
resp.DATA_LENGTH > 0 ? resp.DATA_LENGTH : undefined;
}
if (columnType) {
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
}
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
});
});
return entities;

View File

@ -105,15 +105,16 @@ export default class PostgresDriver extends AbstractDriver {
resp.data_type === "ARRAY"
) {
TomgUtils.LogError(
`Unknown ${resp.data_type} column type: ${resp.udt_name} table name: ${resp.table_name} column name: ${resp.column_name}`
`Unknown ${resp.data_type} column type: ${resp.udt_name} table name: ${resp.table_name} column name: ${resp.column_name}`
);
} else {
TomgUtils.LogError(
`Unknown column type: ${resp.data_type} table name: ${resp.table_name} column name: ${resp.column_name}`
`Unknown column type: ${resp.data_type} table name: ${resp.table_name} column name: ${resp.column_name}`
);
}
return;
}
const columnType = columnTypes.sqlType;
let tscType = columnTypes.tsType;
if (columnTypes.isArray) options.array = true;
@ -177,7 +178,7 @@ export default class PostgresDriver extends AbstractDriver {
isArray: boolean;
enumValues: string[];
} = {
tsType: "NonNullable<unknown>",
tsType: "",
sqlType: dataType,
isArray: false,
enumValues: []
@ -391,6 +392,9 @@ export default class PostgresDriver extends AbstractDriver {
break;
}
break;
default:
ret.tsType = "NonNullable<unknown>";
break;
}
return ret;
}

View File

@ -65,7 +65,7 @@ export default class SqliteDriver extends AbstractDriver {
}>(`PRAGMA table_info('${ent.tscName}');`);
response.forEach(resp => {
const tscName = resp.name;
let tscType = "NonNullable<unknown>";
let tscType = "";
const options: Column["options"] = { name: resp.name };
if (resp.notnull === 0) options.nullable = true;
const isPrimary = resp.pk > 0 ? true : undefined;
@ -164,6 +164,7 @@ export default class SqliteDriver extends AbstractDriver {
tscType = "Date";
break;
default:
tscType = "NonNullable<unknown>";
TomgUtils.LogError(
`Unknown column type: ${columnType} table name: ${ent.tscName} column name: ${resp.name}`
);
@ -218,17 +219,15 @@ export default class SqliteDriver extends AbstractDriver {
);
}
if (columnType) {
ent.columns.push({
generated,
primary: isPrimary,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
}
ent.columns.push({
generated,
primary: isPrimary,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
});
})
);

View File

@ -74,7 +74,7 @@ describe("MssqlDriver", () => {
COLUMN_DEFAULT: "'a'",
COLUMN_NAME: "name",
DATA_TYPE: "int",
IS_NULLABLE: "YES",
IS_NULLABLE: "NO",
NUMERIC_PRECISION: 0,
NUMERIC_SCALE: 0,
IsIdentity: 1
@ -99,7 +99,6 @@ describe("MssqlDriver", () => {
const expected: Entity[] = JSON.parse(JSON.stringify(entities));
expected[0].columns.push({
options: {
nullable: true,
name: "name"
},
type: "int",

View File

@ -0,0 +1,15 @@
import { PrimaryGeneratedColumn, Column, Entity, OneToOne, JoinColumn, Index } from "typeorm";
@Entity("Post")
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column("varchar", { nullable: true })
title: string | null;
@Column()
text: string;
}