Skip to content

Commit fa20c04

Browse files
chore: wip
1 parent de9667a commit fa20c04

File tree

17 files changed

+693
-435
lines changed

17 files changed

+693
-435
lines changed

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

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export async function generateModelString(
2525

2626
let fieldString = ''
2727
let getFields = ''
28-
let constructorFields = ''
28+
// const constructorFields = ''
2929
let jsonFields = '{\n'
3030
let jsonRelations = ''
31-
let declareFields = ''
31+
// let declareFields = ''
3232
let uuidQuery = ''
3333
let whereStatements = ''
3434
let whereFunctionStatements = ''
@@ -166,11 +166,11 @@ export async function generateModelString(
166166
if (relationType === 'hasType' && relationCount === 'many') {
167167
const relationName = camelCase(relation.relationName || tableRelation)
168168

169-
declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model[] | undefined\n`
169+
// declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model[] | undefined\n`
170170
getFields += `get ${snakeCase(relationName)}():${modelRelation}Model[] | undefined {
171171
return this.attributes.${snakeCase(relationName)}
172-
}`
173-
constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
172+
}\n\n`
173+
// constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
174174
fieldString += `${snakeCase(relationName)}?: ${modelRelation}Model[] | undefined\n`
175175

176176
relationStringThisMany += `
@@ -216,22 +216,22 @@ export async function generateModelString(
216216
const relationName = camelCase(relation.relationName || formattedModelRelation)
217217

218218
fieldString += ` ${relation.modelKey}?: number \n`
219-
declareFields += `public ${relation.modelKey}: number | undefined \n `
219+
// declareFields += `public ${relation.modelKey}: number | undefined \n `
220220

221221
getFields += `get ${relation.modelKey}(): number | undefined {
222222
return this.attributes.${relation.modelKey}
223-
}`
223+
}\n\n`
224224

225-
constructorFields += `this.${relation.modelKey} = ${formattedModelName}?.${relation.modelKey}\n `
225+
// constructorFields += `this.${relation.modelKey} = ${formattedModelName}?.${relation.modelKey}\n `
226226
jsonRelations += `${relation.modelKey}: this.${relation.modelKey},\n `
227227

228-
declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model | undefined\n`
228+
// declareFields += `public ${snakeCase(relationName)}: ${modelRelation}Model | undefined\n`
229229

230230
getFields += `get ${snakeCase(relationName)}(): ${modelRelation}Model | undefined {
231231
return this.attributes.${snakeCase(relationName)}
232-
}`
232+
}\n\n`
233233

234-
constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
234+
// constructorFields += `this.${snakeCase(relationName)} = ${formattedModelName}?.${snakeCase(relationName)}\n`
235235
fieldString += `${snakeCase(relationName)}?: ${modelRelation}Model\n`
236236

237237
relationStringThisBelong += `
@@ -284,13 +284,13 @@ export async function generateModelString(
284284
}
285285
}
286286

287-
declareFields += `public id: number | undefined \n `
287+
// declareFields += `public id: number | undefined \n `
288288

289289
getFields += `get id(): number | undefined {
290290
return this.attributes.id
291-
}`
291+
}\n\n`
292292

293-
constructorFields += `this.id = ${formattedModelName}?.id || 1\n `
293+
// constructorFields += `this.id = ${formattedModelName}?.id || 1\n `
294294

295295
const useTwoFactor = typeof model.traits?.useAuth === 'object' && model.traits.useAuth.useTwoFactor
296296
const usePasskey = typeof model.traits?.useAuth === 'object' && model.traits.useAuth.usePasskey
@@ -560,23 +560,23 @@ export async function generateModelString(
560560
}
561561
`
562562

563-
declareFields += `public stripe_id: string | undefined\n`
563+
// declareFields += `public stripe_id: string | undefined\n`
564564

565565
getFields += `get stripe_id(): string | undefined {
566566
return this.attributes.stripe_id
567-
}`
567+
}\n\n`
568568

569-
constructorFields += `this.stripe_id = ${formattedModelName}?.stripe_id\n `
569+
// constructorFields += `this.stripe_id = ${formattedModelName}?.stripe_id\n `
570570
}
571571

572572
if (useTwoFactor) {
573-
declareFields += `public two_factor_secret: string | undefined \n`
573+
// declareFields += `public two_factor_secret: string | undefined \n`
574574

575575
getFields += `get two_factor_secret(): string | undefined {
576576
return this.attributes.two_factor_secret
577-
}`
577+
}\n\n`
578578

579-
constructorFields += `this.two_factor_secret = ${formattedModelName}?.two_factor_secret\n `
579+
// constructorFields += `this.two_factor_secret = ${formattedModelName}?.two_factor_secret\n `
580580

581581
twoFactorStatements += `
582582
async generateTwoFactorForModel() {
@@ -599,31 +599,31 @@ export async function generateModelString(
599599
}
600600

601601
if (useUuid) {
602-
declareFields += 'public uuid: string | undefined \n'
602+
// declareFields += 'public uuid: string | undefined \n'
603603
getFields += `get uuid(): string | undefined {
604604
return this.attributes.uuid
605-
}`
606-
constructorFields += `this.uuid = ${formattedModelName}?.uuid\n `
605+
}\n\n`
606+
// constructorFields += `this.uuid = ${formattedModelName}?.uuid\n `
607607
}
608608

609609
if (usePasskey) {
610-
declareFields += 'public public_passkey: string | undefined \n'
610+
// declareFields += 'public public_passkey: string | undefined \n'
611611
getFields += `get public_passkey(): string | undefined {
612612
return this.attributes.public_passkey
613-
}`
614-
constructorFields += `this.public_passkey = ${formattedModelName}?.public_passkey\n `
613+
}\n\n`
614+
// constructorFields += `this.public_passkey = ${formattedModelName}?.public_passkey\n `
615615
}
616616

617617
jsonFields += '\nid: this.id,\n'
618618
for (const attribute of attributes) {
619619
const entity = mapEntity(attribute)
620620

621621
fieldString += ` ${snakeCase(attribute.field)}?: ${entity}\n `
622-
declareFields += `public ${snakeCase(attribute.field)}: ${entity} | undefined \n `
622+
// declareFields += `public ${snakeCase(attribute.field)}: ${entity} | undefined \n `
623623
getFields += `get ${snakeCase(attribute.field)}(): ${entity} | undefined {
624624
return this.attributes.${snakeCase(attribute.field)}
625-
}`
626-
constructorFields += `this.${snakeCase(attribute.field)} = ${formattedModelName}?.${snakeCase(attribute.field)}\n `
625+
}\n\n`
626+
// constructorFields += `this.${snakeCase(attribute.field)} = ${formattedModelName}?.${snakeCase(attribute.field)}\n `
627627
jsonFields += `${snakeCase(attribute.field)}: this.${snakeCase(attribute.field)},\n `
628628

629629
whereStatements += `static where${pascalCase(attribute.field)}(value: string): ${modelName}Model {
@@ -643,10 +643,10 @@ export async function generateModelString(
643643
}
644644

645645
if (useTimestamps) {
646-
declareFields += `
647-
public created_at: Date | undefined
648-
public updated_at: Date | undefined
649-
`
646+
// declareFields += `
647+
// public created_at: Date | undefined
648+
// public updated_at: Date | undefined
649+
// `
650650

651651
getFields += `get created_at(): Date | undefined {
652652
return this.attributes.created_at
@@ -656,10 +656,10 @@ export async function generateModelString(
656656
return this.attributes.updated_at
657657
}`
658658

659-
constructorFields += `
660-
this.created_at = ${formattedModelName}?.created_at\n
661-
this.updated_at = ${formattedModelName}?.updated_at\n
662-
`
659+
// constructorFields += `
660+
// this.created_at = ${formattedModelName}?.created_at\n
661+
// this.updated_at = ${formattedModelName}?.updated_at\n
662+
// `
663663

664664
jsonFields += `
665665
created_at: this.created_at,\n
@@ -668,16 +668,16 @@ export async function generateModelString(
668668
}
669669

670670
if (useSoftDeletes) {
671-
declareFields += `
672-
public deleted_at: Date | undefined
673-
`
671+
// declareFields += `
672+
// public deleted_at: Date | undefined
673+
// `
674674
getFields += `get deleted_at(): Date | undefined {
675675
return this.attributes.deleted_at
676-
}`
676+
}\n\n`
677677

678-
constructorFields += `
679-
this.deleted_at = ${formattedModelName}?.deleted_at\n
680-
`
678+
// constructorFields += `
679+
// this.deleted_at = ${formattedModelName}?.deleted_at\n
680+
// `
681681

682682
jsonFields += `
683683
deleted_at: this.deleted_at,\n
@@ -770,17 +770,17 @@ export async function generateModelString(
770770
private readonly hidden: Array<keyof ${modelName}JsonResponse> = ${hidden}
771771
private readonly fillable: Array<keyof ${modelName}JsonResponse> = ${fillable}
772772
private readonly guarded: Array<keyof ${modelName}JsonResponse> = ${guarded}
773+
protected attributes: Partial<UserType> = {}
774+
protected originalAttributes: Partial<UserType> = {}
773775
${privateSoftDeletes}
774776
protected selectFromQuery: any
775777
protected withRelations: string[]
776778
protected updateFromQuery: any
777779
protected deleteFromQuery: any
778780
protected hasSelect: boolean
779781
private customColumns: Record<string, unknown> = {}
780-
${declareFields}
781782
constructor(${formattedModelName}: Partial<${modelName}Type> | null) {
782783
if (${formattedModelName}) {
783-
${constructorFields}
784784
785785
Object.keys(${formattedModelName}).forEach(key => {
786786
if (!(key in this)) {
@@ -795,6 +795,8 @@ export async function generateModelString(
795795
this.deleteFromQuery = DB.instance.deleteFrom('${tableName}')
796796
this.hasSelect = false
797797
}
798+
799+
${getFields}
798800
799801
select(params: (keyof ${modelName}Type)[] | RawBuilder<string> | string): ${modelName}Model {
800802
return ${modelName}Model.select(params)

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,17 @@ export class AccessTokenModel {
5656
private readonly hidden: Array<keyof AccessTokenJsonResponse> = []
5757
private readonly fillable: Array<keyof AccessTokenJsonResponse> = ['name', 'token', 'plain_text_token', 'abilities', 'uuid', 'team_id']
5858
private readonly guarded: Array<keyof AccessTokenJsonResponse> = []
59+
protected attributes: Partial<UserType> = {}
60+
protected originalAttributes: Partial<UserType> = {}
5961

6062
protected selectFromQuery: any
6163
protected withRelations: string[]
6264
protected updateFromQuery: any
6365
protected deleteFromQuery: any
6466
protected hasSelect: boolean
6567
private customColumns: Record<string, unknown> = {}
66-
public team_id: number | undefined
67-
public team: TeamModel | undefined
68-
public id: number | undefined
69-
public name: string | undefined
70-
public token: string | undefined
71-
public plain_text_token: string | undefined
72-
public abilities: string[] | undefined
73-
74-
public created_at: Date | undefined
75-
public updated_at: Date | undefined
76-
7768
constructor(accesstoken: Partial<AccessTokenType> | null) {
7869
if (accesstoken) {
79-
this.team_id = accesstoken?.team_id
80-
this.team = accesstoken?.team
81-
this.id = accesstoken?.id || 1
82-
this.name = accesstoken?.name
83-
this.token = accesstoken?.token
84-
this.plain_text_token = accesstoken?.plain_text_token
85-
this.abilities = accesstoken?.abilities
86-
87-
this.created_at = accesstoken?.created_at
88-
89-
this.updated_at = accesstoken?.updated_at
90-
9170
Object.keys(accesstoken).forEach((key) => {
9271
if (!(key in this)) {
9372
this.customColumns[key] = (accesstoken as AccessTokenJsonResponse)[key]
@@ -102,6 +81,42 @@ export class AccessTokenModel {
10281
this.hasSelect = false
10382
}
10483

84+
get team_id(): number | undefined {
85+
return this.attributes.team_id
86+
}
87+
88+
get team(): TeamModel | undefined {
89+
return this.attributes.team
90+
}
91+
92+
get id(): number | undefined {
93+
return this.attributes.id
94+
}
95+
96+
get name(): string | undefined {
97+
return this.attributes.name
98+
}
99+
100+
get token(): string | undefined {
101+
return this.attributes.token
102+
}
103+
104+
get plain_text_token(): string | undefined {
105+
return this.attributes.plain_text_token
106+
}
107+
108+
get abilities(): string[] | undefined {
109+
return this.attributes.abilities
110+
}
111+
112+
get created_at(): Date | undefined {
113+
return this.attributes.created_at
114+
}
115+
116+
get updated_at(): Date | undefined {
117+
return this.attributes.updated_at
118+
}
119+
105120
select(params: (keyof AccessTokenType)[] | RawBuilder<string> | string): AccessTokenModel {
106121
return AccessTokenModel.select(params)
107122
}

0 commit comments

Comments
 (0)