@@ -3,14 +3,15 @@ import _ from "lodash";
33import path from "path" ;
44import util from "util" ;
55import { 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" ;
77const mkdirp = require ( 'mkdirp' ) ;
88
99/** Writes text into files from TableData.text, and writes init-models */
1010export class AutoWriter {
1111 tableText : { [ name : string ] : string } ;
1212 foreignKeys : { [ tableName : string ] : { [ fieldName : string ] : FKSpec } } ;
1313 relations : Relation [ ] ;
14+ space : string [ ] ;
1415 options : {
1516 caseFile ?: CaseFileOption ;
1617 caseModel ?: CaseOption ;
@@ -22,12 +23,15 @@ export class AutoWriter {
2223 noWrite ?: boolean ;
2324 singularize ?: boolean ;
2425 useDefine ?: boolean ;
26+ spaces ?: boolean ;
27+ indentation ?: number ;
2528 } ;
2629 constructor ( tableData : TableData , options : AutoOptions ) {
2730 this . tableText = tableData . text as { [ name : string ] : string } ;
2831 this . foreignKeys = tableData . foreignKeys ;
2932 this . relations = tableData . relations ;
3033 this . options = options ;
34+ this . space = makeIndent ( this . options . spaces , this . options . indentation ) ;
3135 }
3236
3337 write ( ) {
@@ -94,23 +98,24 @@ export class AutoWriter {
9498 private createAssociations ( typeScript : boolean ) {
9599 let strBelongs = "" ;
96100 let strBelongsToMany = "" ;
101+ const sp = this . space [ 1 ] ;
97102
98103 const rels = this . relations ;
99104 rels . forEach ( rel => {
100105 if ( rel . isM2M ) {
101106 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` ;
103108 } else {
104109 // const bAlias = (this.options.noAlias && rel.parentModel.toLowerCase() === rel.parentProp.toLowerCase()) ? '' : `as: "${rel.parentProp}", `;
105110 const asParentProp = recase ( this . options . caseProp , rel . parentProp ) ;
106111 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` ;
108113
109114 const hasRel = rel . isOne ? "hasOne" : "hasMany" ;
110115 // const hAlias = (this.options.noAlias && Utils.pluralize(rel.childModel.toLowerCase()) === rel.childProp.toLowerCase()) ? '' : `as: "${rel.childProp}", `;
111116 const asChildProp = recase ( this . options . caseProp , rel . childProp ) ;
112117 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` ;
114119 }
115120 } ) ;
116121
@@ -121,6 +126,7 @@ export class AutoWriter {
121126 // create the TypeScript init-models file to load all the models into Sequelize
122127 private createTsInitString ( tables : string [ ] , assoc : string ) {
123128 let str = 'import type { Sequelize } from "sequelize";\n' ;
129+ const sp = this . space [ 1 ] ;
124130 const modelNames : string [ ] = [ ] ;
125131 // import statements
126132 tables . forEach ( t => {
@@ -133,33 +139,33 @@ export class AutoWriter {
133139 // re-export the model classes
134140 str += '\nexport {\n' ;
135141 modelNames . forEach ( m => {
136- str += ` _${ m } as ${ m } ,\n` ;
142+ str += `${ sp } _${ m } as ${ m } ,\n` ;
137143 } ) ;
138144 str += '};\n' ;
139145
140146 // re-export the model attirbutes
141147 str += '\nexport type {\n' ;
142148 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` ;
145151 } ) ;
146152 str += '};\n\n' ;
147153
148154 // create the initialization function
149155 str += 'export function initModels(sequelize: Sequelize) {\n' ;
150156 modelNames . forEach ( m => {
151- str += ` const ${ m } = _${ m } .initModel(sequelize);\n` ;
157+ str += `${ sp } const ${ m } = _${ m } .initModel(sequelize);\n` ;
152158 } ) ;
153159
154160 // add the asociations
155161 str += "\n" + assoc ;
156162
157163 // return the models
158- str += "\n return {\n" ;
164+ str += `\n ${ sp } return {\n` ;
159165 modelNames . forEach ( m => {
160- str += ` ${ m } : ${ m } ,\n` ;
166+ str += `${ this . space [ 2 ] } ${ m } : ${ m } ,\n` ;
161167 } ) ;
162- str += ' } ;\n' ;
168+ str += ` ${ sp } } ;\n` ;
163169 str += '}\n' ;
164170
165171 return str ;
@@ -168,6 +174,7 @@ export class AutoWriter {
168174 // create the ES5 init-models file to load all the models into Sequelize
169175 private createES5InitString ( tables : string [ ] , assoc : string , vardef : string ) {
170176 let str = `${ vardef } DataTypes = require("sequelize").DataTypes;\n` ;
177+ const sp = this . space [ 1 ] ;
171178 const modelNames : string [ ] = [ ] ;
172179 // import statements
173180 tables . forEach ( t => {
@@ -180,18 +187,18 @@ export class AutoWriter {
180187 // create the initialization function
181188 str += '\nfunction initModels(sequelize) {\n' ;
182189 modelNames . forEach ( m => {
183- str += ` ${ vardef } ${ m } = _${ m } (sequelize, DataTypes);\n` ;
190+ str += `${ sp } ${ vardef } ${ m } = _${ m } (sequelize, DataTypes);\n` ;
184191 } ) ;
185192
186193 // add the asociations
187194 str += "\n" + assoc ;
188195
189196 // return the models
190- str += "\n return {\n" ;
197+ str += `\n ${ sp } return {\n` ;
191198 modelNames . forEach ( m => {
192- str += ` ${ m } ,\n` ;
199+ str += `${ this . space [ 2 ] } ${ m } ,\n` ;
193200 } ) ;
194- str += ' } ;\n' ;
201+ str += ` ${ sp } } ;\n` ;
195202 str += '}\n' ;
196203 str += 'module.exports = initModels;\n' ;
197204 str += 'module.exports.initModels = initModels;\n' ;
@@ -203,6 +210,7 @@ export class AutoWriter {
203210 private createESMInitString ( tables : string [ ] , assoc : string ) {
204211 let str = 'import _sequelize from "sequelize";\n' ;
205212 str += 'const DataTypes = _sequelize.DataTypes;\n' ;
213+ const sp = this . space [ 1 ] ;
206214 const modelNames : string [ ] = [ ] ;
207215 // import statements
208216 tables . forEach ( t => {
@@ -214,18 +222,18 @@ export class AutoWriter {
214222 // create the initialization function
215223 str += '\nexport default function initModels(sequelize) {\n' ;
216224 modelNames . forEach ( m => {
217- str += ` const ${ m } = _${ m } .init(sequelize, DataTypes);\n` ;
225+ str += `${ sp } const ${ m } = _${ m } .init(sequelize, DataTypes);\n` ;
218226 } ) ;
219227
220228 // add the associations
221229 str += "\n" + assoc ;
222230
223231 // return the models
224- str += "\n return {\n" ;
232+ str += `\n ${ sp } return {\n` ;
225233 modelNames . forEach ( m => {
226- str += ` ${ m } ,\n` ;
234+ str += `${ this . space [ 2 ] } ${ m } ,\n` ;
227235 } ) ;
228- str += ' } ;\n' ;
236+ str += ` ${ sp } } ;\n` ;
229237 str += '}\n' ;
230238 return str ;
231239 }
0 commit comments