Skip to content

Commit de9667a

Browse files
chore: wip
1 parent 946fbeb commit de9667a

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export async function generateModelString(
2424
let thisSoftDeleteStatementsUpdateFrom = ''
2525

2626
let fieldString = ''
27+
let getFields = ''
2728
let constructorFields = ''
2829
let jsonFields = '{\n'
2930
let jsonRelations = ''
@@ -166,6 +167,9 @@ export async function generateModelString(
166167
const relationName = camelCase(relation.relationName || tableRelation)
167168

168169
declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model[] | undefined\n`
170+
getFields += `get ${snakeCase(relationName)}():${modelRelation}Model[] | undefined {
171+
return this.attributes.${snakeCase(relationName)}
172+
}`
169173
constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
170174
fieldString += `${snakeCase(relationName)}?: ${modelRelation}Model[] | undefined\n`
171175

@@ -213,10 +217,20 @@ export async function generateModelString(
213217

214218
fieldString += ` ${relation.modelKey}?: number \n`
215219
declareFields += `public ${relation.modelKey}: number | undefined \n `
220+
221+
getFields += `get ${relation.modelKey}(): number | undefined {
222+
return this.attributes.${relation.modelKey}
223+
}`
224+
216225
constructorFields += `this.${relation.modelKey} = ${formattedModelName}?.${relation.modelKey}\n `
217226
jsonRelations += `${relation.modelKey}: this.${relation.modelKey},\n `
218227

219228
declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model | undefined\n`
229+
230+
getFields += `get ${snakeCase(relationName)}(): ${modelRelation}Model | undefined {
231+
return this.attributes.${snakeCase(relationName)}
232+
}`
233+
220234
constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
221235
fieldString += `${snakeCase(relationName)}?: ${modelRelation}Model\n`
222236

@@ -272,6 +286,10 @@ export async function generateModelString(
272286

273287
declareFields += `public id: number | undefined \n `
274288

289+
getFields += `get id(): number | undefined {
290+
return this.attributes.id
291+
}`
292+
275293
constructorFields += `this.id = ${formattedModelName}?.id || 1\n `
276294

277295
const useTwoFactor = typeof model.traits?.useAuth === 'object' && model.traits.useAuth.useTwoFactor
@@ -544,11 +562,20 @@ export async function generateModelString(
544562

545563
declareFields += `public stripe_id: string | undefined\n`
546564

565+
getFields += `get stripe_id(): string | undefined {
566+
return this.attributes.stripe_id
567+
}`
568+
547569
constructorFields += `this.stripe_id = ${formattedModelName}?.stripe_id\n `
548570
}
549571

550572
if (useTwoFactor) {
551573
declareFields += `public two_factor_secret: string | undefined \n`
574+
575+
getFields += `get two_factor_secret(): string | undefined {
576+
return this.attributes.two_factor_secret
577+
}`
578+
552579
constructorFields += `this.two_factor_secret = ${formattedModelName}?.two_factor_secret\n `
553580

554581
twoFactorStatements += `
@@ -573,11 +600,17 @@ export async function generateModelString(
573600

574601
if (useUuid) {
575602
declareFields += 'public uuid: string | undefined \n'
603+
getFields += `get uuid(): string | undefined {
604+
return this.attributes.uuid
605+
}`
576606
constructorFields += `this.uuid = ${formattedModelName}?.uuid\n `
577607
}
578608

579609
if (usePasskey) {
580610
declareFields += 'public public_passkey: string | undefined \n'
611+
getFields += `get public_passkey(): string | undefined {
612+
return this.attributes.public_passkey
613+
}`
581614
constructorFields += `this.public_passkey = ${formattedModelName}?.public_passkey\n `
582615
}
583616

@@ -587,6 +620,9 @@ export async function generateModelString(
587620

588621
fieldString += ` ${snakeCase(attribute.field)}?: ${entity}\n `
589622
declareFields += `public ${snakeCase(attribute.field)}: ${entity} | undefined \n `
623+
getFields += `get ${snakeCase(attribute.field)}(): ${entity} | undefined {
624+
return this.attributes.${snakeCase(attribute.field)}
625+
}`
590626
constructorFields += `this.${snakeCase(attribute.field)} = ${formattedModelName}?.${snakeCase(attribute.field)}\n `
591627
jsonFields += `${snakeCase(attribute.field)}: this.${snakeCase(attribute.field)},\n `
592628

@@ -612,6 +648,14 @@ export async function generateModelString(
612648
public updated_at: Date | undefined
613649
`
614650

651+
getFields += `get created_at(): Date | undefined {
652+
return this.attributes.created_at
653+
}\n\n
654+
655+
get updated_at(): Date | undefined {
656+
return this.attributes.updated_at
657+
}`
658+
615659
constructorFields += `
616660
this.created_at = ${formattedModelName}?.created_at\n
617661
this.updated_at = ${formattedModelName}?.updated_at\n
@@ -625,8 +669,11 @@ export async function generateModelString(
625669

626670
if (useSoftDeletes) {
627671
declareFields += `
628-
public deleted_at: Date | undefined
629-
`
672+
public deleted_at: Date | undefined
673+
`
674+
getFields += `get deleted_at(): Date | undefined {
675+
return this.attributes.deleted_at
676+
}`
630677

631678
constructorFields += `
632679
this.deleted_at = ${formattedModelName}?.deleted_at\n

0 commit comments

Comments
 (0)