1
- import _ , { isDate } from "lodash" ;
1
+ import _ from "lodash" ;
2
2
import { Utils } from "sequelize" ;
3
3
import { ColumnDescription } from "sequelize/types" ;
4
4
import { DialectOptions , FKSpec } from "./dialects/dialect-options" ;
@@ -59,7 +59,7 @@ export class AutoGenerator {
59
59
header += sp + "super.init({\n" ;
60
60
} else if ( this . options . lang === 'esm' ) {
61
61
header += "import _sequelize from 'sequelize';\n" ;
62
- header += "const { Model, Sequelize } = _sequelize\n\n"
62
+ header += "const { Model, Sequelize } = _sequelize; \n\n" ;
63
63
header += "export default class #TABLE# extends Model {\n" ;
64
64
header += sp + "static init(sequelize, DataTypes) {\n" ;
65
65
header += sp + "super.init({\n" ;
@@ -224,13 +224,6 @@ export class AutoGenerator {
224
224
let defaultVal = fieldObj . defaultValue ;
225
225
const quoteWrapper = '"' ;
226
226
227
- // ENUMs for postgres...
228
- if ( fieldObj . type === "USER-DEFINED" && ! ! fieldObj . special ) {
229
- fieldObj . type = "ENUM(" + fieldObj . special . map ( function ( f : string ) {
230
- return quoteWrapper + f + quoteWrapper ;
231
- } ) . join ( ',' ) + ")" ;
232
- }
233
-
234
227
const unique = fieldObj . unique || fieldObj . foreignKey && fieldObj . foreignKey . isUnique ;
235
228
236
229
const isSerialKey = ( fieldObj . foreignKey && fieldObj . foreignKey . isSerialKey ) ||
@@ -244,7 +237,7 @@ export class AutoGenerator {
244
237
fieldAttrs . forEach ( attr => {
245
238
246
239
// We don't need the special attribute from postgresql; "unique" is handled separately
247
- if ( attr === "special" || attr === "unique" ) {
240
+ if ( attr === "special" || attr === "elementType" || attr === " unique") {
248
241
return true ;
249
242
}
250
243
@@ -341,8 +334,6 @@ export class AutoGenerator {
341
334
342
335
str += space [ 3 ] + attr + ": " + val_text ;
343
336
344
- } else if ( attr === "type" && fieldObj [ attr ] . indexOf ( 'ENUM' ) === 0 ) {
345
- str += space [ 3 ] + attr + ": DataTypes." + fieldObj [ attr ] ;
346
337
} else if ( attr === "comment" && ! fieldObj [ attr ] ) {
347
338
return true ;
348
339
} else {
@@ -470,19 +461,23 @@ export class AutoGenerator {
470
461
} else if ( type . match ( / ^ j s o n / ) ) {
471
462
val = 'DataTypes.JSON' ;
472
463
} else if ( type . match ( / ^ g e o m e t r y / ) ) {
473
- const gtype = fieldObj . special ? `(${ fieldObj . special } )` : '' ;
464
+ const gtype = fieldObj . elementType ? `(${ fieldObj . elementType } )` : '' ;
474
465
val = `DataTypes.GEOMETRY${ gtype } ` ;
475
466
} else if ( type . match ( / ^ g e o g r a p h y / ) ) {
476
- const gtype = fieldObj . special ? `(${ fieldObj . special } )` : '' ;
467
+ const gtype = fieldObj . elementType ? `(${ fieldObj . elementType } )` : '' ;
477
468
val = `DataTypes.GEOGRAPHY${ gtype } ` ;
478
469
} else if ( type . match ( / ^ a r r a y / ) ) {
479
- const eltype = this . getSqType ( fieldObj , "special " ) ;
470
+ const eltype = this . getSqType ( fieldObj , "elementType " ) ;
480
471
val = `DataTypes.ARRAY(${ eltype } )` ;
481
472
} else if ( type . match ( / ( b i n a r y | i m a g e | b l o b ) / ) ) {
482
473
val = 'DataTypes.BLOB' ;
483
474
} else if ( type . match ( / ^ h s t o r e / ) ) {
484
475
val = 'DataTypes.HSTORE' ;
476
+ } else if ( type . match ( / ^ e n u m $ / ) ) {
477
+ const eltype = fieldObj . special . map ( f => `"${ f } "` ) . join ( ', ' ) ;
478
+ val = `DataTypes.ENUM(${ eltype } )` ;
485
479
}
480
+
486
481
return val as string ;
487
482
}
488
483
@@ -512,7 +507,7 @@ export class AutoGenerator {
512
507
const btModelSingular = Utils . singularize ( btModel ) ;
513
508
needed [ btModel ] ??= new Set ( ) ;
514
509
str += `${ sp } // ${ modelName } belongsTo ${ btModel } \n` ;
515
- str += `${ sp } ${ btModelSingular } !: ${ btModel } ;\n`
510
+ str += `${ sp } ${ btModelSingular } !: ${ btModel } ;\n` ;
516
511
str += `${ sp } get${ btModelSingular } !: Sequelize.BelongsToGetAssociationMixin<${ btModel } >;\n` ;
517
512
str += `${ sp } set${ btModelSingular } !: Sequelize.BelongsToSetAssociationMixin<${ btModel } , ${ btModel } Id>;\n` ;
518
513
str += `${ sp } create${ btModelSingular } !: Sequelize.BelongsToCreateAssociationMixin<${ btModel } >;\n` ;
@@ -528,7 +523,7 @@ export class AutoGenerator {
528
523
if ( isOne ) {
529
524
const hasModelSingular = Utils . singularize ( hasModel ) ;
530
525
str += `${ sp } // ${ modelName } hasOne ${ hasModel } \n` ;
531
- str += `${ sp } ${ hasModelSingular } !: ${ hasModel } ;\n`
526
+ str += `${ sp } ${ hasModelSingular } !: ${ hasModel } ;\n` ;
532
527
str += `${ sp } get${ hasModelSingular } !: Sequelize.HasOneGetAssociationMixin<${ hasModel } >;\n` ;
533
528
str += `${ sp } set${ hasModelSingular } !: Sequelize.HasOneSetAssociationMixin<${ hasModel } , ${ hasModel } Id>;\n` ;
534
529
str += `${ sp } create${ hasModelSingular } !: Sequelize.HasOneCreateAssociationMixin<${ hasModel } CreationAttributes>;\n` ;
@@ -539,7 +534,7 @@ export class AutoGenerator {
539
534
const hasModelSingular = Utils . singularize ( hasModel ) ;
540
535
const hasModelPlural = Utils . pluralize ( hasModel ) ;
541
536
str += `${ sp } // ${ modelName } hasMany ${ hasModel } \n` ;
542
- str += `${ sp } ${ hasModelPlural } !: ${ hasModel } [];\n`
537
+ str += `${ sp } ${ hasModelPlural } !: ${ hasModel } [];\n` ;
543
538
str += `${ sp } get${ hasModelPlural } !: Sequelize.HasManyGetAssociationsMixin<${ hasModel } >;\n` ;
544
539
str += `${ sp } set${ hasModelPlural } !: Sequelize.HasManySetAssociationsMixin<${ hasModel } , ${ hasModel } Id>;\n` ;
545
540
str += `${ sp } add${ hasModelSingular } !: Sequelize.HasManyAddAssociationMixin<${ hasModel } , ${ hasModel } Id>;\n` ;
@@ -564,7 +559,7 @@ export class AutoGenerator {
564
559
const otherModelSingular = Utils . singularize ( otherModel ) ;
565
560
const otherModelPlural = Utils . pluralize ( otherModel ) ;
566
561
str += `${ sp } // ${ modelName } belongsToMany ${ otherModel } \n` ;
567
- str += `${ sp } ${ otherModelPlural } !: ${ otherModel } [];\n`
562
+ str += `${ sp } ${ otherModelPlural } !: ${ otherModel } [];\n` ;
568
563
str += `${ sp } get${ otherModelPlural } !: Sequelize.BelongsToManyGetAssociationsMixin<${ otherModel } >;\n` ;
569
564
str += `${ sp } set${ otherModelPlural } !: Sequelize.BelongsToManySetAssociationsMixin<${ otherModel } , ${ otherModel } Id>;\n` ;
570
565
str += `${ sp } add${ otherModelSingular } !: Sequelize.BelongsToManyAddAssociationMixin<${ otherModel } , ${ otherModel } Id>;\n` ;
@@ -614,22 +609,22 @@ export class AutoGenerator {
614
609
}
615
610
616
611
private getTypeScriptFieldType ( fieldObj : any , attr : string ) {
617
- const rawFieldType = fieldObj [ attr ] || ''
612
+ const rawFieldType = fieldObj [ attr ] || '' ;
618
613
const fieldType = rawFieldType . toLowerCase ( ) ;
619
614
let jsType : string ;
620
- if ( this . isString ( fieldType ) ) {
621
- jsType = 'string' ;
622
- } else if ( this . isNumber ( fieldType ) ) {
615
+ if ( this . isNumber ( fieldType ) ) {
623
616
jsType = 'number' ;
624
617
} else if ( this . isBoolean ( fieldType ) ) {
625
618
jsType = 'boolean' ;
626
619
} else if ( this . isDate ( fieldType ) ) {
627
620
jsType = 'Date' ;
621
+ } else if ( this . isString ( fieldType ) ) {
622
+ jsType = 'string' ;
628
623
} else if ( this . isArray ( fieldType ) ) {
629
- const eltype = this . getTypeScriptFieldType ( fieldObj , "special " ) ;
624
+ const eltype = this . getTypeScriptFieldType ( fieldObj , "elementType " ) ;
630
625
jsType = eltype + '[]' ;
631
- } else if ( this . isEnum ( fieldType ) ) {
632
- const values = rawFieldType . substring ( 5 , rawFieldType . length - 1 ) . split ( ',' ) . join ( ' | ' ) ;
626
+ } else if ( this . isEnum ( fieldType ) && fieldObj . special ) {
627
+ const values = fieldObj . special . map ( ( v : string ) => `" ${ v } "` ) . join ( ' | ' ) ;
633
628
jsType = values ;
634
629
} else {
635
630
console . log ( `Missing TypeScript type: ${ fieldType } ` ) ;
@@ -688,7 +683,7 @@ export class AutoGenerator {
688
683
}
689
684
690
685
private isString ( fieldType : string ) : boolean {
691
- return / ^ ( c h a r | n c h a r | s t r i n g | v a r y i n g | v a r c h a r | n v a r c h a r | t e x t | l o n g t e x t | m e d i u m t e x t | t i n y t e x t | n t e x t | u u i d | u n i q u e i d e n t i f i e r | d a t e | t i m e ) (?: \( | $ ) / . test ( fieldType ) ;
686
+ return / ^ ( c h a r | n c h a r | s t r i n g | v a r y i n g | v a r c h a r | n v a r c h a r | t e x t | l o n g t e x t | m e d i u m t e x t | t i n y t e x t | n t e x t | u u i d | u n i q u e i d e n t i f i e r | d a t e | t i m e ) / . test ( fieldType ) ;
692
687
}
693
688
694
689
private isArray ( fieldType : string ) : boolean {
0 commit comments