@@ -984,6 +984,7 @@ export async function generateModelString(
984
984
const formattedTableName = pascalCase ( tableName ) // users -> Users
985
985
const formattedModelName = modelName . toLowerCase ( ) // User -> user
986
986
987
+ let relationDeclare = ''
987
988
let relationString = ''
988
989
989
990
let instanceSoftDeleteStatements = ''
@@ -993,6 +994,7 @@ export async function generateModelString(
993
994
let thisSoftDeleteStatementsUpdateFrom = ''
994
995
995
996
let fieldString = ''
997
+ let hasBelongsType = false
996
998
let constructorFields = ''
997
999
let jsonFields = '{\n'
998
1000
let declareFields = ''
@@ -1092,10 +1094,6 @@ export async function generateModelString(
1092
1094
}
1093
1095
}
1094
1096
1095
- relationString += `
1096
- const instance = new ${ modelName } Model(model as ${ modelName } Type)\n
1097
- `
1098
-
1099
1097
for ( const relation of relations ) {
1100
1098
const modelRelation = relation . model
1101
1099
const foreignKeyRelation = relation . foreignKey
@@ -1173,18 +1171,19 @@ export async function generateModelString(
1173
1171
}
1174
1172
1175
1173
if ( relationType === 'belongsType' && ! relationCount ) {
1174
+ hasBelongsType = true
1176
1175
const relationName = camelCase ( relation . relationName || formattedModelRelation )
1177
1176
1178
1177
declareFields += `public ${ snakeCase ( relationName ) } : any\n`
1179
1178
constructorFields += `this.${ snakeCase ( relationName ) } = ${ formattedModelName } ?.${ snakeCase ( relationName ) } \n`
1180
1179
fieldString += `${ snakeCase ( relationName ) } ?: any\n`
1181
1180
relationString += `
1182
- model.${ relationName } = await instance.${ relationName } ()\n
1181
+ model.${ snakeCase ( relationName ) } = await instance.${ relationName } Belong ()\n
1183
1182
`
1184
- jsonFields += `${ relationName } : this.${ relationName } ,\n`
1183
+ jsonFields += `${ snakeCase ( relationName ) } : this.${ snakeCase ( relationName ) } ,\n`
1185
1184
1186
1185
relationMethods += `
1187
- async ${ relationName } () {
1186
+ async ${ relationName } Belong () {
1188
1187
if (this.${ modelKeyRelation } === undefined)
1189
1188
throw new HttpError(500, 'Relation Error!')
1190
1189
@@ -1225,6 +1224,12 @@ export async function generateModelString(
1225
1224
}
1226
1225
}
1227
1226
1227
+ if ( hasBelongsType ) {
1228
+ relationDeclare += `
1229
+ const instance = new ${ modelName } Model(model as ${ modelName } Type)\n
1230
+ `
1231
+ }
1232
+
1228
1233
declareFields += `public id: number \n `
1229
1234
1230
1235
constructorFields += `this.id = ${ formattedModelName } ?.id || 1\n `
@@ -2101,7 +2106,7 @@ export async function generateModelString(
2101
2106
2102
2107
if (! model)
2103
2108
return undefined
2104
-
2109
+ ${ relationDeclare }
2105
2110
${ relationString }
2106
2111
2107
2112
const data = new ${ modelName } Model(model as ${ modelName } Type)
0 commit comments