@zuzjs/orm 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -103,8 +103,8 @@ class MySqlDriver {
|
|
|
103
103
|
"tinyint": { tsType: "boolean", columnType: "tinyint" },
|
|
104
104
|
"smallint": { tsType: "number", columnType: "smallint" },
|
|
105
105
|
"mediumint": { tsType: "number", columnType: "mediumint" },
|
|
106
|
-
"bigint": { tsType: "
|
|
107
|
-
"decimal": { tsType: "
|
|
106
|
+
"bigint": { tsType: "number", columnType: "bigint" }, // bigint is safer as string
|
|
107
|
+
"decimal": { tsType: "number", columnType: "decimal" },
|
|
108
108
|
"float": { tsType: "number", columnType: "float" },
|
|
109
109
|
"double": { tsType: "number", columnType: "double" },
|
|
110
110
|
"varchar": { tsType: "string", columnType: "varchar", length: 255 },
|
|
@@ -117,6 +117,13 @@ class MySqlDriver {
|
|
|
117
117
|
"time": { tsType: "string", columnType: "time" },
|
|
118
118
|
"json": { tsType: "any", columnType: "json" },
|
|
119
119
|
};
|
|
120
|
+
const enumMatch = sqlType.match(/^enum\((.*)\)$/i);
|
|
121
|
+
if (enumMatch) {
|
|
122
|
+
const enumValues = enumMatch[1]
|
|
123
|
+
.split(",")
|
|
124
|
+
.map((val) => val.trim().replace(/^'|'$/g, "")); // Remove single quotes
|
|
125
|
+
return { tsType: `"${enumValues.join('" | "')}"`, columnType: "enum", enumValues };
|
|
126
|
+
}
|
|
120
127
|
const match = sqlType.match(/^(\w+)(?:\((\d+)\))?/);
|
|
121
128
|
if (!match)
|
|
122
129
|
return { tsType: "any", columnType: "varchar", length: 255 };
|
|
@@ -150,6 +157,7 @@ class MySqlDriver {
|
|
|
150
157
|
foreignKeys[tableName] = fkResults;
|
|
151
158
|
}
|
|
152
159
|
for (const tableName of tableNames) {
|
|
160
|
+
const enums = [];
|
|
153
161
|
const imports = [];
|
|
154
162
|
const _imports = [`Entity`, `BaseEntity`];
|
|
155
163
|
// Get table structure
|
|
@@ -160,7 +168,13 @@ class MySqlDriver {
|
|
|
160
168
|
for (const column of columns) {
|
|
161
169
|
// console.log(tableName, column)
|
|
162
170
|
const { Field, Type, Key, Null, Default, Extra, Comment } = column;
|
|
163
|
-
const { tsType, columnType, length } = this.mapColumns(Type);
|
|
171
|
+
const { tsType, columnType, length, enumValues } = this.mapColumns(Type);
|
|
172
|
+
let enumName = null;
|
|
173
|
+
if (columnType === "enum" && enumValues) {
|
|
174
|
+
enumName = (0, index_js_1.toPascalCase)(Field);
|
|
175
|
+
enums.push(`export enum ${enumName} { ${enumValues.map(v => `${(0, index_js_1.toPascalCase)(v)} = "${v}"`).join(", ")} }`);
|
|
176
|
+
// entityCode.push(`\t@Column({ type: "enum", enum: ${enumName} })\n`);
|
|
177
|
+
}
|
|
164
178
|
// Handle primary key
|
|
165
179
|
if (Key === "PRI") {
|
|
166
180
|
const _priColumn = Extra.includes("auto_increment") ? `PrimaryGeneratedColumn` : `PrimaryColumn`;
|
|
@@ -179,6 +193,8 @@ class MySqlDriver {
|
|
|
179
193
|
columnDecorator += `, length: ${length}`;
|
|
180
194
|
if (Null === "YES")
|
|
181
195
|
columnDecorator += `, nullable: true`;
|
|
196
|
+
if (enumName)
|
|
197
|
+
columnDecorator += `, enum: ${enumName}`;
|
|
182
198
|
if (Default !== null)
|
|
183
199
|
columnDecorator += `, default: ${this.formatDefault(Default, tsType)}`;
|
|
184
200
|
columnDecorator += ` })`;
|
|
@@ -187,7 +203,7 @@ class MySqlDriver {
|
|
|
187
203
|
if (Comment && Comment.length > 0) {
|
|
188
204
|
entityCode.push(`\t/** @comment ${Comment} */`);
|
|
189
205
|
}
|
|
190
|
-
entityCode.push(`\t${Field}!: ${Key == `PRI` && numberTypes.includes(Type) ? `number` : numberTypes.includes(Type) ? `number` : tsType};\n`);
|
|
206
|
+
entityCode.push(`\t${Field}!: ${enumName ? enumName : Key == `PRI` && numberTypes.includes(Type) ? `number` : numberTypes.includes(Type) ? `number` : tsType};\n`);
|
|
191
207
|
}
|
|
192
208
|
// Add foreign key relationships
|
|
193
209
|
if (foreignKeys[tableName]) {
|
|
@@ -210,7 +226,8 @@ class MySqlDriver {
|
|
|
210
226
|
`*/`,
|
|
211
227
|
`import { ${_imports.join(`, `)} } from "@zuzjs/orm";`,
|
|
212
228
|
imports.length > 0 ? imports.join(`\n`) : ``,
|
|
213
|
-
|
|
229
|
+
enums.length > 0 ? enums.join(`\n`) : ``,
|
|
230
|
+
`${enums.length > 0 || imports.length > 0 ? `\n` : ``}@Entity({ name: "${tableName}" })`,
|
|
214
231
|
`export class ${(0, index_js_1.toPascalCase)(tableName)} extends BaseEntity {\n`,
|
|
215
232
|
...entityCode,
|
|
216
233
|
`}`
|