@@ -15,13 +15,18 @@ const initArgParser = (ArgParserClass) => {
15
15
)
16
16
17
17
parser . addArgument (
18
- [ '-a' , '--associationName' ] ,
19
- { help : 'array of name of associations' , nargs : '+' } ,
18
+ [ '-a' , '--associationModelName' ] ,
19
+ { help : 'array of name of model of associations' , nargs : '+' } ,
20
+ ) ;
21
+
22
+ parser . addArgument (
23
+ [ '-s' , '--singular' ] ,
24
+ { help : 'array of singular of alias names (the alias for association, may be same as associationModelName)' , nargs : '+' }
20
25
) ;
21
26
22
27
parser . addArgument (
23
28
[ '-p' , '--plural' ] ,
24
- { help : 'array of plural of association names (if n/a then use _)' , nargs : '+' } ,
29
+ { help : 'array of plural of alias names (if n/a then use _)' , nargs : '+' } ,
25
30
) ;
26
31
27
32
parser . addArgument (
@@ -38,15 +43,15 @@ const initArgParser = (ArgParserClass) => {
38
43
}
39
44
40
45
// FIXME: this doesn't seem to work
41
- const assertArgLengthsAreTheSame = ( args ) => {
42
- [ "associationName ", "plural" , "type" , "joinTable" ] . map ( k => console . log ( args [ k ] . length ) ) ;
43
- console . log ( args . associationName . length === args . plural . length === args . type . length === args . joinTable . length ) ;
44
- return args . associationName . length === args . plural . length === args . type . length === args . joinTable . length ;
45
- } ;
46
+ // const assertArgLengthsAreTheSame = (args) => {
47
+ // ["associationModelName ", "plural", "type", "joinTable"].map(k => console.log(args[k].length));
48
+ // console.log(args.associationModelName .length === args.plural.length === args.type.length === args.joinTable.length);
49
+ // return args.associationModelName .length === args.plural.length === args.type.length === args.joinTable.length;
50
+ // };
46
51
47
- const addMixinsBelongsTo = ( singular ) => {
48
- const AssocInstance = `${ singular } Instance` ;
49
- const AssocAttributes = `${ singular } Attributes` ;
52
+ const addMixinsBelongsTo = ( associationModelName , singular ) => {
53
+ const AssocInstance = `${ associationModelName } Instance` ;
54
+ const AssocAttributes = `${ associationModelName } Attributes` ;
50
55
return (
51
56
`
52
57
get${ singular } : Sequelize.BelongsToGetAssociationMixin<${ AssocInstance } >;
@@ -56,9 +61,9 @@ const addMixinsBelongsTo = (singular) => {
56
61
) ;
57
62
} ;
58
63
59
- const addMixinsHasOne = ( singular ) => {
60
- const AssocInstance = `${ singular } Instance` ;
61
- const AssocAttributes = `${ singular } Attributes` ;
64
+ const addMixinsHasOne = ( associationModelName , singular ) => {
65
+ const AssocInstance = `${ associationModelName } Instance` ;
66
+ const AssocAttributes = `${ associationModelName } Attributes` ;
62
67
return (
63
68
`
64
69
get${ singular } : Sequelize.HasOneGetAssociationMixin<${ AssocInstance } >;
@@ -68,9 +73,9 @@ const addMixinsHasOne = (singular) => {
68
73
) ;
69
74
} ;
70
75
71
- const addMixinsHasMany = ( singular , plural ) => {
72
- const AssocInstance = `${ singular } Instance` ;
73
- const AssocAttributes = `${ singular } Attributes` ;
76
+ const addMixinsHasMany = ( associationModelName , singular , plural ) => {
77
+ const AssocInstance = `${ associationModelName } Instance` ;
78
+ const AssocAttributes = `${ associationModelName } Attributes` ;
74
79
return (
75
80
`
76
81
get${ plural } : Sequelize.HasManyGetAssociationsMixin<${ AssocInstance } >;
@@ -87,9 +92,9 @@ const addMixinsHasMany = (singular, plural) => {
87
92
) ;
88
93
} ;
89
94
90
- const addMixinsBelongsToMany = ( singular , plural , joinTableName ) => {
91
- const AssocInstance = `${ singular } Instance` ;
92
- const AssocAttributes = `${ singular } Attributes` ;
95
+ const addMixinsBelongsToMany = ( associationModelName , singular , plural , joinTableName ) => {
96
+ const AssocInstance = `${ associationModelName } Instance` ;
97
+ const AssocAttributes = `${ associationModelName } Attributes` ;
93
98
const JoinTableAttributes = joinTableName [ 0 ] === '\"' || joinTableName [ 0 ] === "\'"
94
99
? joinTableName
95
100
: `${ joinTableName } Attributes` ;
@@ -110,17 +115,17 @@ const addMixinsBelongsToMany = (singular, plural, joinTableName) => {
110
115
) ;
111
116
} ;
112
117
113
- const generateInterface = ( { baseModelName, associationName , plural, type, joinTable } ) => {
118
+ const generateInterface = ( { baseModelName, associationModelName , singular , plural, type, joinTable } ) => {
114
119
let mixinsString = '' ;
115
- for ( let i = 0 ; i < associationName . length ; i ++ ) {
120
+ for ( let i = 0 ; i < associationModelName . length ; i ++ ) {
116
121
if ( type [ i ] === "BelongsTo" ) {
117
- mixinsString += addMixinsBelongsTo ( associationName [ i ] ) ;
122
+ mixinsString += addMixinsBelongsTo ( associationModelName [ i ] , singular [ i ] ) ;
118
123
} else if ( type [ i ] === "HasOne" ) {
119
- mixinsString += addMixinsHasOne ( associationName [ i ] ) ;
124
+ mixinsString += addMixinsHasOne ( associationModelName [ i ] , singular [ i ] ) ;
120
125
} else if ( type [ i ] === "HasMany" ) {
121
- mixinsString += addMixinsHasMany ( associationName [ i ] , plural [ i ] ) ;
126
+ mixinsString += addMixinsHasMany ( associationModelName [ i ] , singular [ i ] , plural [ i ] ) ;
122
127
} else if ( type [ i ] === "BelongsToMany" ) {
123
- mixinsString += addMixinsBelongsToMany ( associationName [ i ] , plural [ i ] , joinTable [ i ] ) ;
128
+ mixinsString += addMixinsBelongsToMany ( associationModelName [ i ] , singular [ i ] , plural [ i ] , joinTable [ i ] ) ;
124
129
} else {
125
130
console . error ( "incorrect type: " , type [ i ] ) ;
126
131
return ;
0 commit comments