@@ -208,49 +208,100 @@ const getAttributeType = (attr: Eloquent.Attribute): string => {
208
208
return type ;
209
209
} ;
210
210
211
- const mapType = ( type : string ) : string => {
212
- const mapping : Record < string , ( string | RegExp ) [ ] > = {
213
- bool : [
214
- "boolean(1)" ,
215
- "boolean(0)" ,
216
- "tinyint" ,
217
- "tinyint unsigned" ,
218
- "boolean" ,
219
- / t i n y i n t \( \d + \) / ,
220
- ] ,
221
- string : [
222
- "longtext" ,
223
- "mediumtext" ,
224
- "text" ,
225
- / v a r c h a r \( \d + \) / ,
226
- / c h a r \( \d + \) / ,
227
- ] ,
228
- float : [ / d o u b l e \( \d + \, \d + \) / ] ,
229
- int : [ "bigint" , "bigint unsigned" , "integer" , "int unsigned" ] ,
230
- mixed : [ "attribute" , "accessor" , "encrypted" ] ,
231
- array : [ "encrypted:json" , "encrypted:array" , "json" ] ,
232
- "\\Illuminate\\Support\\Carbon" : [ "datetime" , "timestamp" ] ,
233
- "\\Illuminate\\Support\\Collection" : [ "encrypted:collection" ] ,
234
- object : [ "encrypted:object" ] ,
235
- } ;
211
+ const castMapping : Record < string , ( string | RegExp ) [ ] > = {
212
+ array : [
213
+ "json" ,
214
+ "encrypted:json" ,
215
+ "encrypted:array" ,
216
+ ] ,
217
+ int : [
218
+ "timestamp" ,
219
+ ] ,
220
+ mixed : [
221
+ "attribute" ,
222
+ "accessor" ,
223
+ "encrypted" ,
224
+ ] ,
225
+ object : [
226
+ "encrypted:object" ,
227
+ ] ,
228
+ string : [
229
+ "hashed" ,
230
+ ] ,
231
+ "\\Illuminate\\Support\\Carbon" : [
232
+ "date" ,
233
+ "datetime" ,
234
+ ] ,
235
+ "\\Illuminate\\Support\\Collection" : [
236
+ "encrypted:collection" ,
237
+ ] ,
238
+ } ;
239
+
240
+ const typeMapping : Record < string , ( string | RegExp ) [ ] > = {
241
+ bool : [
242
+ / ^ b o o l e a n ( \( ( 0 | 1 ) \) ) ? $ / ,
243
+ / ^ t i n y i n t ( u n s i g n e d ) ? ( \( \d + \) ) ? $ / ,
244
+ ] ,
245
+ float : [
246
+ "real" ,
247
+ "money" ,
248
+ "double precision" ,
249
+ / ^ ( d o u b l e | d e c i m a l | n u m e r i c ) ( \( \d + \, \d + \) ) ? $ / ,
250
+ ] ,
251
+ int : [
252
+ / ^ ( b i g ) ? s e r i a l $ / ,
253
+ / ^ ( s m a l l | b i g ) ? i n t ( e g e r ) ? ( u n s i g n e d ) ? $ / ,
254
+ ] ,
255
+ resource : [
256
+ "bytea" ,
257
+ ] ,
258
+ string : [
259
+ "box" ,
260
+ "cidr" ,
261
+ "date" ,
262
+ "inet" ,
263
+ "line" ,
264
+ "lseg" ,
265
+ "path" ,
266
+ "time" ,
267
+ "uuid" ,
268
+ "point" ,
269
+ "circle" ,
270
+ "polygon" ,
271
+ "interval" ,
272
+ / ^ j s o n ( b ) ? $ / ,
273
+ / ^ m a c a d d r ( 8 ) ? $ / ,
274
+ / ^ ( l o n g | m e d i u m ) ? t e x t $ / ,
275
+ / ^ ( v a r ) ? c h a r ( a c t e r ) ? ( v a r y i n g ) ? ?( \( \d + \) ) ? $ / ,
276
+ / ^ t i m e ( s t a m p ) ? \( \d + \) ( ( w i t h | w i t h o u t ) t i m e z o n e ) ? $ / ,
277
+ ] ,
278
+ } ;
279
+
280
+ const findInMapping = (
281
+ mapping : Record < string , ( string | RegExp ) [ ] > ,
282
+ value : string | null
283
+ ) : string | null => {
284
+ if ( value === null ) {
285
+ return null ;
286
+ }
236
287
237
288
for ( const [ newType , matches ] of Object . entries ( mapping ) ) {
238
289
for ( const match of matches ) {
239
- if ( type === match ) {
290
+ if ( match === value ) {
240
291
return newType ;
241
292
}
242
293
243
- if ( match instanceof RegExp && type . match ( match ) ) {
294
+ if ( match instanceof RegExp && value . match ( match ) ) {
244
295
return newType ;
245
296
}
246
297
}
247
298
}
248
299
249
- return type ;
300
+ return null ;
250
301
} ;
251
302
252
- const getActualType = ( type : string ) : string => {
253
- const finalType = mapType ( type ) ;
303
+ const getActualType = ( cast : string | null , type : string ) : string => {
304
+ const finalType = findInMapping ( castMapping , cast ) ?? findInMapping ( typeMapping , type ) ?? "mixed" ;
254
305
255
306
if ( finalType . includes ( "\\" ) && ! finalType . startsWith ( "\\" ) ) {
256
307
return `\\${ finalType } ` ;
0 commit comments