@@ -3,14 +3,15 @@ import _ from "lodash";
3
3
import path from "path" ;
4
4
import util from "util" ;
5
5
import { FKSpec , TableData } from "." ;
6
- import { AutoOptions , CaseFileOption , CaseOption , LangOption , makeTableName , pluralize , qNameSplit , recase , Relation } from "./types" ;
6
+ import { AutoOptions , CaseFileOption , CaseOption , LangOption , makeIndent , makeTableName , pluralize , qNameSplit , recase , Relation } from "./types" ;
7
7
const mkdirp = require ( 'mkdirp' ) ;
8
8
9
9
/** Writes text into files from TableData.text, and writes init-models */
10
10
export class AutoWriter {
11
11
tableText : { [ name : string ] : string } ;
12
12
foreignKeys : { [ tableName : string ] : { [ fieldName : string ] : FKSpec } } ;
13
13
relations : Relation [ ] ;
14
+ space : string [ ] ;
14
15
options : {
15
16
caseFile ?: CaseFileOption ;
16
17
caseModel ?: CaseOption ;
@@ -22,12 +23,15 @@ export class AutoWriter {
22
23
noWrite ?: boolean ;
23
24
singularize ?: boolean ;
24
25
useDefine ?: boolean ;
26
+ spaces ?: boolean ;
27
+ indentation ?: number ;
25
28
} ;
26
29
constructor ( tableData : TableData , options : AutoOptions ) {
27
30
this . tableText = tableData . text as { [ name : string ] : string } ;
28
31
this . foreignKeys = tableData . foreignKeys ;
29
32
this . relations = tableData . relations ;
30
33
this . options = options ;
34
+ this . space = makeIndent ( this . options . spaces , this . options . indentation ) ;
31
35
}
32
36
33
37
write ( ) {
@@ -94,23 +98,24 @@ export class AutoWriter {
94
98
private createAssociations ( typeScript : boolean ) {
95
99
let strBelongs = "" ;
96
100
let strBelongsToMany = "" ;
101
+ const sp = this . space [ 1 ] ;
97
102
98
103
const rels = this . relations ;
99
104
rels . forEach ( rel => {
100
105
if ( rel . isM2M ) {
101
106
const asprop = recase ( this . options . caseProp , pluralize ( rel . childProp ) ) ;
102
- strBelongsToMany += ` ${ rel . parentModel } .belongsToMany(${ rel . childModel } , { as: '${ asprop } ', through: ${ rel . joinModel } , foreignKey: "${ rel . parentId } ", otherKey: "${ rel . childId } " });\n` ;
107
+ strBelongsToMany += `${ sp } ${ rel . parentModel } .belongsToMany(${ rel . childModel } , { as: '${ asprop } ', through: ${ rel . joinModel } , foreignKey: "${ rel . parentId } ", otherKey: "${ rel . childId } " });\n` ;
103
108
} else {
104
109
// const bAlias = (this.options.noAlias && rel.parentModel.toLowerCase() === rel.parentProp.toLowerCase()) ? '' : `as: "${rel.parentProp}", `;
105
110
const asParentProp = recase ( this . options . caseProp , rel . parentProp ) ;
106
111
const bAlias = this . options . noAlias ? '' : `as: "${ asParentProp } ", ` ;
107
- strBelongs += ` ${ rel . childModel } .belongsTo(${ rel . parentModel } , { ${ bAlias } foreignKey: "${ rel . parentId } "});\n` ;
112
+ strBelongs += `${ sp } ${ rel . childModel } .belongsTo(${ rel . parentModel } , { ${ bAlias } foreignKey: "${ rel . parentId } "});\n` ;
108
113
109
114
const hasRel = rel . isOne ? "hasOne" : "hasMany" ;
110
115
// const hAlias = (this.options.noAlias && Utils.pluralize(rel.childModel.toLowerCase()) === rel.childProp.toLowerCase()) ? '' : `as: "${rel.childProp}", `;
111
116
const asChildProp = recase ( this . options . caseProp , rel . childProp ) ;
112
117
const hAlias = this . options . noAlias ? '' : `as: "${ asChildProp } ", ` ;
113
- strBelongs += ` ${ rel . parentModel } .${ hasRel } (${ rel . childModel } , { ${ hAlias } foreignKey: "${ rel . parentId } "});\n` ;
118
+ strBelongs += `${ sp } ${ rel . parentModel } .${ hasRel } (${ rel . childModel } , { ${ hAlias } foreignKey: "${ rel . parentId } "});\n` ;
114
119
}
115
120
} ) ;
116
121
@@ -121,6 +126,7 @@ export class AutoWriter {
121
126
// create the TypeScript init-models file to load all the models into Sequelize
122
127
private createTsInitString ( tables : string [ ] , assoc : string ) {
123
128
let str = 'import type { Sequelize } from "sequelize";\n' ;
129
+ const sp = this . space [ 1 ] ;
124
130
const modelNames : string [ ] = [ ] ;
125
131
// import statements
126
132
tables . forEach ( t => {
@@ -133,33 +139,33 @@ export class AutoWriter {
133
139
// re-export the model classes
134
140
str += '\nexport {\n' ;
135
141
modelNames . forEach ( m => {
136
- str += ` _${ m } as ${ m } ,\n` ;
142
+ str += `${ sp } _${ m } as ${ m } ,\n` ;
137
143
} ) ;
138
144
str += '};\n' ;
139
145
140
146
// re-export the model attirbutes
141
147
str += '\nexport type {\n' ;
142
148
modelNames . forEach ( m => {
143
- str += ` ${ m } Attributes,\n` ;
144
- str += ` ${ m } CreationAttributes,\n` ;
149
+ str += `${ sp } ${ m } Attributes,\n` ;
150
+ str += `${ sp } ${ m } CreationAttributes,\n` ;
145
151
} ) ;
146
152
str += '};\n\n' ;
147
153
148
154
// create the initialization function
149
155
str += 'export function initModels(sequelize: Sequelize) {\n' ;
150
156
modelNames . forEach ( m => {
151
- str += ` const ${ m } = _${ m } .initModel(sequelize);\n` ;
157
+ str += `${ sp } const ${ m } = _${ m } .initModel(sequelize);\n` ;
152
158
} ) ;
153
159
154
160
// add the asociations
155
161
str += "\n" + assoc ;
156
162
157
163
// return the models
158
- str += "\n return {\n" ;
164
+ str += `\n ${ sp } return {\n` ;
159
165
modelNames . forEach ( m => {
160
- str += ` ${ m } : ${ m } ,\n` ;
166
+ str += `${ this . space [ 2 ] } ${ m } : ${ m } ,\n` ;
161
167
} ) ;
162
- str += ' } ;\n' ;
168
+ str += ` ${ sp } } ;\n` ;
163
169
str += '}\n' ;
164
170
165
171
return str ;
@@ -168,6 +174,7 @@ export class AutoWriter {
168
174
// create the ES5 init-models file to load all the models into Sequelize
169
175
private createES5InitString ( tables : string [ ] , assoc : string , vardef : string ) {
170
176
let str = `${ vardef } DataTypes = require("sequelize").DataTypes;\n` ;
177
+ const sp = this . space [ 1 ] ;
171
178
const modelNames : string [ ] = [ ] ;
172
179
// import statements
173
180
tables . forEach ( t => {
@@ -180,18 +187,18 @@ export class AutoWriter {
180
187
// create the initialization function
181
188
str += '\nfunction initModels(sequelize) {\n' ;
182
189
modelNames . forEach ( m => {
183
- str += ` ${ vardef } ${ m } = _${ m } (sequelize, DataTypes);\n` ;
190
+ str += `${ sp } ${ vardef } ${ m } = _${ m } (sequelize, DataTypes);\n` ;
184
191
} ) ;
185
192
186
193
// add the asociations
187
194
str += "\n" + assoc ;
188
195
189
196
// return the models
190
- str += "\n return {\n" ;
197
+ str += `\n ${ sp } return {\n` ;
191
198
modelNames . forEach ( m => {
192
- str += ` ${ m } ,\n` ;
199
+ str += `${ this . space [ 2 ] } ${ m } ,\n` ;
193
200
} ) ;
194
- str += ' } ;\n' ;
201
+ str += ` ${ sp } } ;\n` ;
195
202
str += '}\n' ;
196
203
str += 'module.exports = initModels;\n' ;
197
204
str += 'module.exports.initModels = initModels;\n' ;
@@ -203,6 +210,7 @@ export class AutoWriter {
203
210
private createESMInitString ( tables : string [ ] , assoc : string ) {
204
211
let str = 'import _sequelize from "sequelize";\n' ;
205
212
str += 'const DataTypes = _sequelize.DataTypes;\n' ;
213
+ const sp = this . space [ 1 ] ;
206
214
const modelNames : string [ ] = [ ] ;
207
215
// import statements
208
216
tables . forEach ( t => {
@@ -214,18 +222,18 @@ export class AutoWriter {
214
222
// create the initialization function
215
223
str += '\nexport default function initModels(sequelize) {\n' ;
216
224
modelNames . forEach ( m => {
217
- str += ` const ${ m } = _${ m } .init(sequelize, DataTypes);\n` ;
225
+ str += `${ sp } const ${ m } = _${ m } .init(sequelize, DataTypes);\n` ;
218
226
} ) ;
219
227
220
228
// add the associations
221
229
str += "\n" + assoc ;
222
230
223
231
// return the models
224
- str += "\n return {\n" ;
232
+ str += `\n ${ sp } return {\n` ;
225
233
modelNames . forEach ( m => {
226
- str += ` ${ m } ,\n` ;
234
+ str += `${ this . space [ 2 ] } ${ m } ,\n` ;
227
235
} ) ;
228
- str += ' } ;\n' ;
236
+ str += ` ${ sp } } ;\n` ;
229
237
str += '}\n' ;
230
238
return str ;
231
239
}
0 commit comments