Skip to content

Commit cb2853a

Browse files
chore: wip
1 parent 7e82f7e commit cb2853a

File tree

15 files changed

+26
-39
lines changed

15 files changed

+26
-39
lines changed

storage/framework/core/orm/src/utils.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ export async function generateModelString(
984984
const formattedTableName = pascalCase(tableName) // users -> Users
985985
const formattedModelName = modelName.toLowerCase() // User -> user
986986

987+
let relationDeclare = ''
987988
let relationString = ''
988989

989990
let instanceSoftDeleteStatements = ''
@@ -993,6 +994,7 @@ export async function generateModelString(
993994
let thisSoftDeleteStatementsUpdateFrom = ''
994995

995996
let fieldString = ''
997+
let hasBelongsType = false
996998
let constructorFields = ''
997999
let jsonFields = '{\n'
9981000
let declareFields = ''
@@ -1092,10 +1094,6 @@ export async function generateModelString(
10921094
}
10931095
}
10941096

1095-
relationString += `
1096-
const instance = new ${modelName}Model(model as ${modelName}Type)\n
1097-
`
1098-
10991097
for (const relation of relations) {
11001098
const modelRelation = relation.model
11011099
const foreignKeyRelation = relation.foreignKey
@@ -1173,18 +1171,19 @@ export async function generateModelString(
11731171
}
11741172

11751173
if (relationType === 'belongsType' && !relationCount) {
1174+
hasBelongsType = true
11761175
const relationName = camelCase(relation.relationName || formattedModelRelation)
11771176

11781177
declareFields += `public ${snakeCase(relationName)}: any\n`
11791178
constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
11801179
fieldString += `${snakeCase(relationName)}?: any\n`
11811180
relationString += `
1182-
model.${relationName} = await instance.${relationName}()\n
1181+
model.${snakeCase(relationName)} = await instance.${relationName}Belong()\n
11831182
`
1184-
jsonFields += `${relationName}: this.${relationName},\n`
1183+
jsonFields += `${snakeCase(relationName)}: this.${snakeCase(relationName)},\n`
11851184

11861185
relationMethods += `
1187-
async ${relationName}() {
1186+
async ${relationName}Belong() {
11881187
if (this.${modelKeyRelation} === undefined)
11891188
throw new HttpError(500, 'Relation Error!')
11901189
@@ -1225,6 +1224,12 @@ export async function generateModelString(
12251224
}
12261225
}
12271226

1227+
if (hasBelongsType) {
1228+
relationDeclare += `
1229+
const instance = new ${modelName}Model(model as ${modelName}Type)\n
1230+
`
1231+
}
1232+
12281233
declareFields += `public id: number \n `
12291234

12301235
constructorFields += `this.id = ${formattedModelName}?.id || 1\n `
@@ -2101,7 +2106,7 @@ export async function generateModelString(
21012106
21022107
if (! model)
21032108
return undefined
2104-
2109+
${relationDeclare}
21052110
${relationString}
21062111
21072112
const data = new ${modelName}Model(model as ${modelName}Type)

storage/framework/orm/src/models/AccessToken.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,6 @@ export class AccessTokenModel {
490490
if (!model)
491491
return undefined
492492

493-
const instance = new AccessTokenModel(model as AccessTokenType)
494-
495493
const data = new AccessTokenModel(model as AccessTokenType)
496494

497495
return data

storage/framework/orm/src/models/Deployment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ export class DeploymentModel {
538538

539539
const instance = new DeploymentModel(model as DeploymentType)
540540

541-
model.user = await instance.user()
541+
model.user = await instance.userBelong()
542542

543543
const data = new DeploymentModel(model as DeploymentType)
544544

@@ -656,7 +656,7 @@ export class DeploymentModel {
656656
.execute()
657657
}
658658

659-
async user() {
659+
async userBelong() {
660660
if (this.user_id === undefined)
661661
throw new HttpError(500, 'Relation Error!')
662662

storage/framework/orm/src/models/Error.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,6 @@ export class ErrorModel {
506506
if (!model)
507507
return undefined
508508

509-
const instance = new ErrorModel(model as ErrorType)
510-
511509
const data = new ErrorModel(model as ErrorType)
512510

513511
return data

storage/framework/orm/src/models/PaymentMethod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ export class PaymentMethodModel {
543543

544544
const instance = new PaymentMethodModel(model as PaymentMethodType)
545545

546-
model.user = await instance.user()
546+
model.user = await instance.userBelong()
547547

548548
const data = new PaymentMethodModel(model as PaymentMethodType)
549549

@@ -661,7 +661,7 @@ export class PaymentMethodModel {
661661
.execute()
662662
}
663663

664-
async user() {
664+
async userBelong() {
665665
if (this.user_id === undefined)
666666
throw new HttpError(500, 'Relation Error!')
667667

storage/framework/orm/src/models/Post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ export class PostModel {
473473

474474
const instance = new PostModel(model as PostType)
475475

476-
model.user = await instance.user()
476+
model.user = await instance.userBelong()
477477

478478
const data = new PostModel(model as PostType)
479479

@@ -591,7 +591,7 @@ export class PostModel {
591591
.execute()
592592
}
593593

594-
async user() {
594+
async userBelong() {
595595
if (this.user_id === undefined)
596596
throw new HttpError(500, 'Relation Error!')
597597

storage/framework/orm/src/models/Product.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,6 @@ export class ProductModel {
527527
if (!model)
528528
return undefined
529529

530-
const instance = new ProductModel(model as ProductType)
531-
532530
const data = new ProductModel(model as ProductType)
533531

534532
return data

storage/framework/orm/src/models/Project.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,6 @@ export class ProjectModel {
484484
if (!model)
485485
return undefined
486486

487-
const instance = new ProjectModel(model as ProjectType)
488-
489487
const data = new ProjectModel(model as ProjectType)
490488

491489
return data

storage/framework/orm/src/models/Release.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,6 @@ export class ReleaseModel {
451451
if (!model)
452452
return undefined
453453

454-
const instance = new ReleaseModel(model as ReleaseType)
455-
456454
const data = new ReleaseModel(model as ReleaseType)
457455

458456
return data

storage/framework/orm/src/models/Subscriber.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,6 @@ export class SubscriberModel {
455455
if (!model)
456456
return undefined
457457

458-
const instance = new SubscriberModel(model as SubscriberType)
459-
460458
const data = new SubscriberModel(model as SubscriberType)
461459

462460
return data

0 commit comments

Comments
 (0)