Nullable columns now also add "| null" to the type declaration.

Unknown DB types now generate properties with type "NonNullable<unknown>" that also adds "| null" if the column is nullable.
This commit is contained in:
Vasil Rangelov 2019-11-14 11:46:18 +02:00
parent c2d1e038a5
commit 3bbabd127b
7 changed files with 29 additions and 30 deletions

View File

@ -376,11 +376,19 @@ export default abstract class AbstractDriver {
ownerColumns[0].tscName,
ownerEntity
);
let relationIdType = ownerColumns[0].tscType;
if (ownerColumns[0].options.nullable) {
relationIdType += " | null";
}
ownerEntity.relationIds.push({
fieldName: relationIdFieldName,
fieldType: isOneToMany
? `${ownerColumns[0].tscType}[]`
: ownerColumns[0].tscType,
? `${
relationIdType.includes(" ")
? `(${relationIdType})`
: relationIdType
}[]`
: relationIdType,
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 = "";
let tscType = "NonNullable<unknown>";
switch (resp.DATA_TYPE) {
case "bigint":
tscType = "string";

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 = "";
let tscType = "NonNullable<unknown>";
const options: Column["options"] = {
name: resp.COLUMN_NAME
};

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 = "";
let tscType = "NonNullable<unknown>";
switch (DATA_TYPE.toLowerCase()) {
case "char":
tscType = "string";

View File

@ -99,7 +99,7 @@ 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"
@ -152,16 +152,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;
@ -173,17 +172,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: "NonNullable<unknown>",
sqlType: dataType,
isArray: false,
enumValues: []
};
ret.sqlType = dataType;
switch (dataType) {
case "int2":
ret.tsType = "number";
@ -389,17 +387,10 @@ 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;
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 = "";
let tscType = "NonNullable<unknown>";
const options: Column["options"] = { name: resp.name };
if (resp.notnull === 0) options.nullable = true;
const isPrimary = resp.pk > 0 ? true : undefined;

View File

@ -6,7 +6,7 @@ import { {{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"}}