-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
generateFieldType.ts
129 lines (120 loc) · 2.94 KB
/
generateFieldType.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { Config } from "~/utils/validateConfig";
type TypeName =
| "BigInt"
| "Boolean"
| "Bytes"
| "DateTime"
| "Decimal"
| "Float"
| "Int"
| "Json"
| "String"
| "Unsupported";
type TypeMap = Partial<Record<TypeName, string>> & { [x: string]: string };
export const sqliteTypeMap: TypeMap = {
BigInt: "number",
Boolean: "number",
Bytes: "Buffer",
DateTime: "string",
Decimal: "number",
Float: "number",
Int: "number",
String: "string",
Unsupported: "unknown",
};
export const mysqlTypeMap: TypeMap = {
BigInt: "number",
Boolean: "number",
Bytes: "Buffer",
DateTime: "Timestamp",
Decimal: "string",
Float: "number",
Int: "number",
Json: "unknown",
String: "string",
Unsupported: "unknown",
};
export const postgresqlTypeMap: TypeMap = {
BigInt: "string",
Boolean: "boolean",
Bytes: "Buffer",
DateTime: "Timestamp",
Decimal: "string",
Float: "number",
Int: "number",
Json: "unknown",
String: "string",
Unsupported: "unknown",
};
export const sqlServerTypeMap: TypeMap = {
BigInt: "number",
Boolean: "boolean",
Bytes: "Buffer",
DateTime: "Timestamp",
Decimal: "string",
Float: "number",
Int: "number",
Json: "unknown",
String: "string",
Unsupported: "unknown",
};
export const overrideType = (type: string, config: Config) => {
switch (type) {
case "String":
return config.stringTypeOverride;
case "DateTime":
return config.dateTimeTypeOverride;
case "Boolean":
return config.booleanTypeOverride;
case "BigInt":
return config.bigIntTypeOverride;
case "Int":
return config.intTypeOverride;
case "Float":
return config.floatTypeOverride;
case "Decimal":
return config.decimalTypeOverride;
case "Bytes":
return config.bytesTypeOverride;
case "Json":
return config.jsonTypeOverride;
case "Unsupported":
return config.unsupportedTypeOverride;
}
};
export const generateFieldTypeInner = (
type: string,
config: Config,
typeOverride: string | null
) => {
switch (config.databaseProvider) {
case "sqlite":
return typeOverride || overrideType(type, config) || sqliteTypeMap[type];
case "mysql":
return typeOverride || overrideType(type, config) || mysqlTypeMap[type];
case "postgresql":
return (
typeOverride || overrideType(type, config) || postgresqlTypeMap[type]
);
case "cockroachdb":
return (
typeOverride || overrideType(type, config) || postgresqlTypeMap[type]
);
case "sqlserver":
return (
typeOverride || overrideType(type, config) || sqlServerTypeMap[type]
);
}
};
export const generateFieldType = (
type: string,
config: Config,
typeOverride?: string | null
) => {
const fieldType = generateFieldTypeInner(type, config, typeOverride || null);
if (!fieldType)
throw new Error(
`Unsupported type ${type} for database ${config.databaseProvider}`
);
return fieldType;
};