|
1 | 1 | import fs from "fs";
|
2 | 2 | import { Eloquent } from "..";
|
3 |
| -import { |
4 |
| - internalVendorPath, |
5 |
| -} from "./project"; |
| 3 | +import { internalVendorPath } from "./project"; |
6 | 4 | import { indent } from "./util";
|
7 | 5 |
|
8 | 6 | interface ClassBlock {
|
@@ -204,33 +202,50 @@ const getAttributeType = (attr: Eloquent.Attribute): string => {
|
204 | 202 | return attr.nullable ? `${type}|null` : type;
|
205 | 203 | };
|
206 | 204 |
|
207 |
| -const getActualType = (type: string): string => { |
208 |
| - const mapping: { |
209 |
| - [key: string]: string; |
210 |
| - } = { |
211 |
| - datetime: "\\Illuminate\\Support\\Carbon", |
212 |
| - "boolean(1)": "bool", |
213 |
| - "boolean(0)": "bool", |
214 |
| - boolean: "bool", |
215 |
| - text: "string", |
216 |
| - bigint: "int", |
217 |
| - "bigint unsigned": "int", |
218 |
| - integer: "int", |
219 |
| - attribute: "mixed", |
220 |
| - accessor: "mixed", |
221 |
| - "encrypted:json": "array", |
222 |
| - "encrypted:array": "array", |
223 |
| - "encrypted:collection": "\\Illuminate\\Support\\Collection", |
224 |
| - "encrypted:object": "object", |
225 |
| - encrypted: "mixed", |
| 205 | +const mapType = (type: string): string => { |
| 206 | + const mapping: Record<string, (string | RegExp)[]> = { |
| 207 | + bool: [ |
| 208 | + "boolean(1)", |
| 209 | + "boolean(0)", |
| 210 | + "tinyint", |
| 211 | + "tinyint unsigned", |
| 212 | + "boolean", |
| 213 | + /tinyint\(\d+\)/, |
| 214 | + ], |
| 215 | + string: [ |
| 216 | + "longtext", |
| 217 | + "mediumtext", |
| 218 | + "text", |
| 219 | + /varchar\(\d+\)/, |
| 220 | + /char\(\d+\)/, |
| 221 | + ], |
| 222 | + float: [/double\(\d+\,\d+\)/], |
| 223 | + int: ["bigint", "bigint unsigned", "integer"], |
| 224 | + mixed: ["attribute", "accessor", "encrypted"], |
| 225 | + array: ["encrypted:json", "encrypted:array", "json"], |
| 226 | + "\\Illuminate\\Support\\Carbon": ["datetime", "timestamp"], |
| 227 | + "\\Illuminate\\Support\\Collection": ["encrypted:collection"], |
| 228 | + object: ["encrypted:object"], |
226 | 229 | };
|
227 | 230 |
|
228 |
| - let finalType = mapping[type] || type; |
| 231 | + for (const [newType, matches] of Object.entries(mapping)) { |
| 232 | + for (const match of matches) { |
| 233 | + if (type === match) { |
| 234 | + return newType; |
| 235 | + } |
229 | 236 |
|
230 |
| - if (finalType.match(/varchar\(\d+\)/)) { |
231 |
| - finalType = "string"; |
| 237 | + if (match instanceof RegExp && type.match(match)) { |
| 238 | + return newType; |
| 239 | + } |
| 240 | + } |
232 | 241 | }
|
233 | 242 |
|
| 243 | + return type; |
| 244 | +}; |
| 245 | + |
| 246 | +const getActualType = (type: string): string => { |
| 247 | + const finalType = mapType(type); |
| 248 | + |
234 | 249 | if (finalType.includes("\\") && !finalType.startsWith("\\")) {
|
235 | 250 | return `\\${finalType}`;
|
236 | 251 | }
|
|
0 commit comments