Skip to content

Commit db325b2

Browse files
chore: wip
1 parent aa5e4c8 commit db325b2

File tree

2 files changed

+10
-29
lines changed

2 files changed

+10
-29
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,10 @@ export async function generateModelString(
202202

203203
if (relationType === 'hasType' && relationCount === 'one') {
204204
const relationName = relation.relationName || formattedModelRelation
205-
relationMethods += `
206-
async ${relationName}() {
207-
if (this.id === undefined)
208-
throw new HttpError(500, 'Relation Error!')
209-
210-
const model = ${modelRelation}
211-
.where('${foreignKeyRelation}', '=', this.id).first()
212-
213-
if (! model)
214-
throw new HttpError(500, 'Model Relation Not Found!')
215-
216-
return model
217-
}\n\n`
205+
206+
getFields += `get ${snakeCase(relationName)}():${modelRelation}Model | undefined {
207+
return this.attributes.${snakeCase(relationName)}
208+
}\n\n`
218209
}
219210

220211
if (relationType === 'belongsType' && !relationCount) {

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type { CheckoutLineItem, CheckoutOptions, StripeCustomerOptions } from '@
33
import type { DeploymentModel } from './Deployment'
44
import type { PaymentMethodModel } from './PaymentMethod'
55
import type { PostModel } from './Post'
6+
import type { SubscriberModel } from './Subscriber'
67
import type { SubscriptionModel } from './Subscription'
78
import type { TransactionModel } from './Transaction'
89
import { randomUUIDv7 } from 'bun'
910
import { cache } from '@stacksjs/cache'
11+
1012
import { sql } from '@stacksjs/database'
1113

1214
import { HttpError, ModelNotFoundException } from '@stacksjs/error-handling'
@@ -17,8 +19,6 @@ import { DB, SubqueryBuilder } from '@stacksjs/orm'
1719

1820
import { manageCharge, manageCheckout, manageCustomer, manageInvoice, managePaymentMethod, manageSetupIntent, manageSubscription, manageTransaction, type Stripe } from '@stacksjs/payments'
1921

20-
import Subscriber from './Subscriber'
21-
2222
import Team from './Team'
2323

2424
export interface UsersTable {
@@ -105,6 +105,10 @@ export class UserModel {
105105
this.hasSaved = false
106106
}
107107

108+
get subscriber(): SubscriberModel | undefined {
109+
return this.attributes.subscriber
110+
}
111+
108112
get deployments(): DeploymentModel[] | undefined {
109113
return this.attributes.deployments
110114
}
@@ -1517,20 +1521,6 @@ export class UserModel {
15171521
.execute()
15181522
}
15191523

1520-
async subscriber() {
1521-
if (this.id === undefined)
1522-
throw new HttpError(500, 'Relation Error!')
1523-
1524-
const model = Subscriber
1525-
.where('user_id', '=', this.id)
1526-
.first()
1527-
1528-
if (!model)
1529-
throw new HttpError(500, 'Model Relation Not Found!')
1530-
1531-
return model
1532-
}
1533-
15341524
async userTeams() {
15351525
if (this.id === undefined)
15361526
throw new HttpError(500, 'Relation Error!')

0 commit comments

Comments
 (0)