Skip to content

Commit 0f0c343

Browse files
Merge pull request #233 from laravel/eloquent-property-types
More Eloquent column mapping types
2 parents fbdba29 + 8f52f03 commit 0f0c343

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

src/support/docblocks.ts

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import fs from "fs";
22
import { Eloquent } from "..";
3-
import {
4-
internalVendorPath,
5-
} from "./project";
3+
import { internalVendorPath } from "./project";
64
import { indent } from "./util";
75

86
interface ClassBlock {
@@ -204,33 +202,50 @@ const getAttributeType = (attr: Eloquent.Attribute): string => {
204202
return attr.nullable ? `${type}|null` : type;
205203
};
206204

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"],
226229
};
227230

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+
}
229236

230-
if (finalType.match(/varchar\(\d+\)/)) {
231-
finalType = "string";
237+
if (match instanceof RegExp && type.match(match)) {
238+
return newType;
239+
}
240+
}
232241
}
233242

243+
return type;
244+
};
245+
246+
const getActualType = (type: string): string => {
247+
const finalType = mapType(type);
248+
234249
if (finalType.includes("\\") && !finalType.startsWith("\\")) {
235250
return `\\${finalType}`;
236251
}

0 commit comments

Comments
 (0)