Null in TS types (for "next") (#227)

Null in TS types (for "next")
This commit is contained in:
Kononnable 2019-12-29 23:23:44 +01:00 committed by GitHub
commit 48a2dfcbae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 71 deletions

View File

@ -369,11 +369,20 @@ export default abstract class AbstractDriver {
ownerColumns[0].tscName,
ownerEntity
);
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
? `${ownerColumns[0].tscType}[]`
: ownerColumns[0].tscType,
fieldType,
relationField: ownerRelation.fieldName
});
// TODO: RelationId on ManyToMany

View File

@ -204,6 +204,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}`
);
@ -230,17 +231,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

@ -237,6 +237,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}`
);
@ -273,16 +274,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

@ -187,6 +187,7 @@ export default class OracleDriver extends AbstractDriver {
tscType = "number";
break;
default:
tscType = "NonNullable<unknown>";
TomgUtils.LogError(
`Unknown column type:${DATA_TYPE}`
);
@ -211,16 +212,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

@ -111,21 +111,22 @@ export default class PostgresDriver extends AbstractDriver {
resp.udt_name,
resp.enumvalues
);
if (!columnTypes.sqlType || !columnTypes.tsType) {
if (columnTypes.tsType === "NonNullable<unknown>") {
if (
resp.data_type === "USER-DEFINED" ||
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;
@ -164,16 +165,15 @@ export default class PostgresDriver extends AbstractDriver {
? resp.character_maximum_length
: undefined;
}
if (columnType && tscType) {
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
}
ent.columns.push({
generated,
type: columnType,
default: defaultValue,
options,
tscName,
tscType
});
});
});
return entities;
@ -185,17 +185,16 @@ export default class PostgresDriver extends AbstractDriver {
enumValues: string | null
) {
let ret: {
tsType?: Column["tscType"];
sqlType: string | null;
tsType: Column["tscType"];
sqlType: string;
isArray: boolean;
enumValues: string[];
} = {
tsType: undefined,
sqlType: null,
tsType: "",
sqlType: dataType,
isArray: false,
enumValues: []
};
ret.sqlType = dataType;
switch (dataType) {
case "int2":
ret.tsType = "number";
@ -401,16 +400,12 @@ export default class PostgresDriver extends AbstractDriver {
.join('" | "')}"` as never) as string;
ret.sqlType = "enum";
ret.enumValues = enumValues.split(",");
} else {
ret.tsType = undefined;
ret.sqlType = null;
}
break;
}
break;
default:
ret.tsType = undefined;
ret.sqlType = null;
ret.tsType = "NonNullable<unknown>";
break;
}
return ret;

View File

@ -172,6 +172,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}`
);
@ -226,17 +227,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

@ -6,7 +6,7 @@ import {{localImport (toEntityName .)}} from './{{toFileName .}}'
{{/inline}}
{{#*inline "Column"}}
{{#generated}}@PrimaryGeneratedColumn({ type:"{{type}}", {{/generated}}{{^generated}}@Column("{{type}}",{ {{#primary}}primary:{{primary}},{{/primary}}{{/generated}}{{json options}}{{#default}},default: {{.}},{{/default}} })
{{printPropertyVisibility}}{{toPropertyName tscName}}{{strictMode}}:{{tscType}};
{{printPropertyVisibility}}{{toPropertyName tscName}}{{strictMode}}:{{tscType}}{{#if options.nullable}} | null{{/if}};
{{/inline}}
{{#*inline "JoinColumnOptions"}}

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;
}