enum type safety #205
This commit is contained in:
parent
3b1d08bf35
commit
ef10b8653a
@ -3,6 +3,7 @@
|
||||
## Unreleased
|
||||
|
||||
* change default case conversions for generated files (#196)
|
||||
* enum type safety #205
|
||||
|
||||
## 0.3.5
|
||||
|
||||
|
@ -165,7 +165,12 @@ export default class MysqlDriver extends AbstractDriver {
|
||||
colInfo.tsType = "string";
|
||||
break;
|
||||
case "enum":
|
||||
colInfo.tsType = "string";
|
||||
colInfo.tsType = resp.COLUMN_TYPE.substring(
|
||||
5,
|
||||
resp.COLUMN_TYPE.length - 1
|
||||
)
|
||||
.replace(/'/gi, '"')
|
||||
.replace(/","/gi, '" | "');
|
||||
colInfo.options.enum = resp.COLUMN_TYPE.substring(
|
||||
5,
|
||||
resp.COLUMN_TYPE.length - 1
|
||||
|
@ -164,21 +164,16 @@ WHERE "n"."nspname" = table_schema AND "t"."typname"=udt_name
|
||||
enumValues: string | null
|
||||
) {
|
||||
let ret: {
|
||||
tsType:
|
||||
| "number"
|
||||
| "string"
|
||||
| "boolean"
|
||||
| "Date"
|
||||
| "Buffer"
|
||||
| "object"
|
||||
| "string | object"
|
||||
| "string | string[]"
|
||||
| "any"
|
||||
| null;
|
||||
tsType?: ColumnInfo["tsType"];
|
||||
sqlType: string | null;
|
||||
isArray: boolean;
|
||||
enumValues: string[];
|
||||
} = { tsType: null, sqlType: null, isArray: false, enumValues: [] };
|
||||
} = {
|
||||
tsType: undefined,
|
||||
sqlType: null,
|
||||
isArray: false,
|
||||
enumValues: []
|
||||
};
|
||||
ret.sqlType = dataType;
|
||||
switch (dataType) {
|
||||
case "int2":
|
||||
@ -380,19 +375,22 @@ WHERE "n"."nspname" = table_schema AND "t"."typname"=udt_name
|
||||
break;
|
||||
default:
|
||||
if (enumValues) {
|
||||
ret.tsType = (`"${enumValues
|
||||
.split(",")
|
||||
.join('" | "')}"` as never) as string;
|
||||
ret.sqlType = "enum";
|
||||
ret.enumValues = (`"${enumValues
|
||||
.split(",")
|
||||
.join('","')}"` as never) as string[];
|
||||
} else {
|
||||
ret.tsType = null;
|
||||
ret.tsType = undefined;
|
||||
ret.sqlType = null;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ret.tsType = null;
|
||||
ret.tsType = undefined;
|
||||
ret.sqlType = null;
|
||||
break;
|
||||
}
|
||||
|
@ -15,7 +15,8 @@ export default class ColumnInfo {
|
||||
| "object"
|
||||
| "string | object"
|
||||
| "string | string[]"
|
||||
| "any";
|
||||
| "any"
|
||||
| string;
|
||||
|
||||
public relations: RelationInfo[] = [];
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ import { Entity, PrimaryColumn, Column } from "typeorm";
|
||||
|
||||
@Entity("Post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn()
|
||||
id: number;
|
||||
|
||||
@ -18,7 +17,7 @@ export class Post {
|
||||
@Column("tinyint")
|
||||
tinyint: number;
|
||||
|
||||
@Column("tinyint",{width:1})
|
||||
@Column("tinyint", { width: 1 })
|
||||
boolean: boolean;
|
||||
|
||||
@Column("smallint")
|
||||
@ -85,7 +84,7 @@ export class Post {
|
||||
longtext: string;
|
||||
|
||||
@Column("enum", { enum: ["A", "B", "C"] })
|
||||
enum: string;
|
||||
enum: "A" | "B" | "C";
|
||||
|
||||
// In mariaDb Json is recognized as longtext
|
||||
// @Column("json")
|
||||
@ -117,5 +116,4 @@ export class Post {
|
||||
|
||||
@Column("geometrycollection")
|
||||
geometrycollection: string;
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ import { Entity, PrimaryColumn, Column } from "typeorm";
|
||||
|
||||
@Entity("Post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn()
|
||||
id: number;
|
||||
|
||||
@ -88,7 +87,7 @@ export class Post {
|
||||
longtext: string;
|
||||
|
||||
@Column("enum", { enum: ["A", "B", "C"] })
|
||||
enum: string;
|
||||
enum: "A" | "B" | "C";
|
||||
|
||||
@Column("json")
|
||||
json: object;
|
||||
|
@ -2,7 +2,6 @@ import { Entity, PrimaryColumn, Column } from "typeorm";
|
||||
|
||||
@Entity("Post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn()
|
||||
id: number;
|
||||
|
||||
@ -120,7 +119,7 @@ export class Post {
|
||||
boolean: boolean;
|
||||
|
||||
@Column("enum", { enum: ["A", "B", "C"] })
|
||||
enum: string;
|
||||
enum: "A" | "B" | "C";
|
||||
|
||||
@Column("point")
|
||||
point: string | object;
|
||||
@ -187,5 +186,4 @@ export class Post {
|
||||
|
||||
@Column("daterange")
|
||||
daterange: string;
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user