Skip to content

Commit 70293e0

Browse files
chore: wip
1 parent a903a43 commit 70293e0

File tree

18 files changed

+380
-166
lines changed

18 files changed

+380
-166
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,28 +1921,38 @@ export async function generateModelString(
19211921
}
19221922
}
19231923
1924-
async loadRelations(models: ${modelName}Model[]): Promise<void> {
1925-
if (!models.length)
1926-
return
1927-
1928-
const modelIds = models.map(model => model.id)
1924+
async loadRelations(models: ${modelName}Model | ${modelName}Model[]): Promise<void> {
1925+
// Handle both single model and array of models
1926+
const modelArray = Array.isArray(models) ? models : [models]
1927+
if (!modelArray.length) return
1928+
1929+
const modelIds = modelArray.map(model => model.id)
19291930
19301931
for (const relation of this.withRelations) {
19311932
const relatedRecords = await DB.instance
19321933
.selectFrom(relation)
19331934
.where('${formattedModelName}_id', 'in', modelIds)
19341935
.selectAll()
19351936
.execute()
1936-
1937-
models.map((model: ${modelName}Model) => {
1937+
1938+
if (Array.isArray(models)) {
1939+
// If array, map through all models
1940+
models.map((model: ${modelName}Model) => {
1941+
const records = relatedRecords.filter((record: any) => {
1942+
return record.${formattedModelName}__id === model.id
1943+
})
1944+
1945+
model[relation] = records.length === 1 ? records[0] : records
1946+
return model
1947+
})
1948+
} else {
1949+
// If single model, just filter once
19381950
const records = relatedRecords.filter((record: any) => {
1939-
return record.${formattedModelName}_id === model.id
1951+
return record.${formattedModelName}__id === models.id
19401952
})
1941-
1942-
model[relation] = records.length === 1 ? records[0] : records
1943-
1944-
return model
1945-
})
1953+
1954+
models[relation] = records.length === 1 ? records[0] : records
1955+
}
19461956
}
19471957
}
19481958

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,11 +1176,13 @@ export class AccessTokenModel {
11761176
}
11771177
}
11781178

1179-
async loadRelations(models: AccessTokenModel[]): Promise<void> {
1180-
if (!models.length)
1179+
async loadRelations(models: AccessTokenModel | AccessTokenModel[]): Promise<void> {
1180+
// Handle both single model and array of models
1181+
const modelArray = Array.isArray(models) ? models : [models]
1182+
if (!modelArray.length)
11811183
return
11821184

1183-
const modelIds = models.map(model => model.id)
1185+
const modelIds = modelArray.map(model => model.id)
11841186

11851187
for (const relation of this.withRelations) {
11861188
const relatedRecords = await DB.instance
@@ -1189,15 +1191,25 @@ export class AccessTokenModel {
11891191
.selectAll()
11901192
.execute()
11911193

1192-
models.map((model: AccessTokenModel) => {
1194+
if (Array.isArray(models)) {
1195+
// If array, map through all models
1196+
models.map((model: AccessTokenModel) => {
1197+
const records = relatedRecords.filter((record: any) => {
1198+
return record.accesstoken__id === model.id
1199+
})
1200+
1201+
model[relation] = records.length === 1 ? records[0] : records
1202+
return model
1203+
})
1204+
}
1205+
else {
1206+
// If single model, just filter once
11931207
const records = relatedRecords.filter((record: any) => {
1194-
return record.accesstoken_id === model.id
1208+
return record.accesstoken__id === models.id
11951209
})
11961210

1197-
model[relation] = records.length === 1 ? records[0] : records
1198-
1199-
return model
1200-
})
1211+
models[relation] = records.length === 1 ? records[0] : records
1212+
}
12011213
}
12021214
}
12031215

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,11 +1226,13 @@ export class ActivityModel {
12261226
}
12271227
}
12281228

1229-
async loadRelations(models: ActivityModel[]): Promise<void> {
1230-
if (!models.length)
1229+
async loadRelations(models: ActivityModel | ActivityModel[]): Promise<void> {
1230+
// Handle both single model and array of models
1231+
const modelArray = Array.isArray(models) ? models : [models]
1232+
if (!modelArray.length)
12311233
return
12321234

1233-
const modelIds = models.map(model => model.id)
1235+
const modelIds = modelArray.map(model => model.id)
12341236

12351237
for (const relation of this.withRelations) {
12361238
const relatedRecords = await DB.instance
@@ -1239,15 +1241,25 @@ export class ActivityModel {
12391241
.selectAll()
12401242
.execute()
12411243

1242-
models.map((model: ActivityModel) => {
1244+
if (Array.isArray(models)) {
1245+
// If array, map through all models
1246+
models.map((model: ActivityModel) => {
1247+
const records = relatedRecords.filter((record: any) => {
1248+
return record.activity__id === model.id
1249+
})
1250+
1251+
model[relation] = records.length === 1 ? records[0] : records
1252+
return model
1253+
})
1254+
}
1255+
else {
1256+
// If single model, just filter once
12431257
const records = relatedRecords.filter((record: any) => {
1244-
return record.activity_id === model.id
1258+
return record.activity__id === models.id
12451259
})
12461260

1247-
model[relation] = records.length === 1 ? records[0] : records
1248-
1249-
return model
1250-
})
1261+
models[relation] = records.length === 1 ? records[0] : records
1262+
}
12511263
}
12521264
}
12531265

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,11 +1241,13 @@ export class DeploymentModel {
12411241
}
12421242
}
12431243

1244-
async loadRelations(models: DeploymentModel[]): Promise<void> {
1245-
if (!models.length)
1244+
async loadRelations(models: DeploymentModel | DeploymentModel[]): Promise<void> {
1245+
// Handle both single model and array of models
1246+
const modelArray = Array.isArray(models) ? models : [models]
1247+
if (!modelArray.length)
12461248
return
12471249

1248-
const modelIds = models.map(model => model.id)
1250+
const modelIds = modelArray.map(model => model.id)
12491251

12501252
for (const relation of this.withRelations) {
12511253
const relatedRecords = await DB.instance
@@ -1254,15 +1256,25 @@ export class DeploymentModel {
12541256
.selectAll()
12551257
.execute()
12561258

1257-
models.map((model: DeploymentModel) => {
1259+
if (Array.isArray(models)) {
1260+
// If array, map through all models
1261+
models.map((model: DeploymentModel) => {
1262+
const records = relatedRecords.filter((record: any) => {
1263+
return record.deployment__id === model.id
1264+
})
1265+
1266+
model[relation] = records.length === 1 ? records[0] : records
1267+
return model
1268+
})
1269+
}
1270+
else {
1271+
// If single model, just filter once
12581272
const records = relatedRecords.filter((record: any) => {
1259-
return record.deployment_id === model.id
1273+
return record.deployment__id === models.id
12601274
})
12611275

1262-
model[relation] = records.length === 1 ? records[0] : records
1263-
1264-
return model
1265-
})
1276+
models[relation] = records.length === 1 ? records[0] : records
1277+
}
12661278
}
12671279
}
12681280

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,13 @@ export class ErrorModel {
11801180
}
11811181
}
11821182

1183-
async loadRelations(models: ErrorModel[]): Promise<void> {
1184-
if (!models.length)
1183+
async loadRelations(models: ErrorModel | ErrorModel[]): Promise<void> {
1184+
// Handle both single model and array of models
1185+
const modelArray = Array.isArray(models) ? models : [models]
1186+
if (!modelArray.length)
11851187
return
11861188

1187-
const modelIds = models.map(model => model.id)
1189+
const modelIds = modelArray.map(model => model.id)
11881190

11891191
for (const relation of this.withRelations) {
11901192
const relatedRecords = await DB.instance
@@ -1193,15 +1195,25 @@ export class ErrorModel {
11931195
.selectAll()
11941196
.execute()
11951197

1196-
models.map((model: ErrorModel) => {
1198+
if (Array.isArray(models)) {
1199+
// If array, map through all models
1200+
models.map((model: ErrorModel) => {
1201+
const records = relatedRecords.filter((record: any) => {
1202+
return record.error__id === model.id
1203+
})
1204+
1205+
model[relation] = records.length === 1 ? records[0] : records
1206+
return model
1207+
})
1208+
}
1209+
else {
1210+
// If single model, just filter once
11971211
const records = relatedRecords.filter((record: any) => {
1198-
return record.error_id === model.id
1212+
return record.error__id === models.id
11991213
})
12001214

1201-
model[relation] = records.length === 1 ? records[0] : records
1202-
1203-
return model
1204-
})
1215+
models[relation] = records.length === 1 ? records[0] : records
1216+
}
12051217
}
12061218
}
12071219

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,13 @@ export class FailedJobModel {
11801180
}
11811181
}
11821182

1183-
async loadRelations(models: FailedJobModel[]): Promise<void> {
1184-
if (!models.length)
1183+
async loadRelations(models: FailedJobModel | FailedJobModel[]): Promise<void> {
1184+
// Handle both single model and array of models
1185+
const modelArray = Array.isArray(models) ? models : [models]
1186+
if (!modelArray.length)
11851187
return
11861188

1187-
const modelIds = models.map(model => model.id)
1189+
const modelIds = modelArray.map(model => model.id)
11881190

11891191
for (const relation of this.withRelations) {
11901192
const relatedRecords = await DB.instance
@@ -1193,15 +1195,25 @@ export class FailedJobModel {
11931195
.selectAll()
11941196
.execute()
11951197

1196-
models.map((model: FailedJobModel) => {
1198+
if (Array.isArray(models)) {
1199+
// If array, map through all models
1200+
models.map((model: FailedJobModel) => {
1201+
const records = relatedRecords.filter((record: any) => {
1202+
return record.failedjob__id === model.id
1203+
})
1204+
1205+
model[relation] = records.length === 1 ? records[0] : records
1206+
return model
1207+
})
1208+
}
1209+
else {
1210+
// If single model, just filter once
11971211
const records = relatedRecords.filter((record: any) => {
1198-
return record.failedjob_id === model.id
1212+
return record.failedjob__id === models.id
11991213
})
12001214

1201-
model[relation] = records.length === 1 ? records[0] : records
1202-
1203-
return model
1204-
})
1215+
models[relation] = records.length === 1 ? records[0] : records
1216+
}
12051217
}
12061218
}
12071219

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,13 @@ export class JobModel {
11801180
}
11811181
}
11821182

1183-
async loadRelations(models: JobModel[]): Promise<void> {
1184-
if (!models.length)
1183+
async loadRelations(models: JobModel | JobModel[]): Promise<void> {
1184+
// Handle both single model and array of models
1185+
const modelArray = Array.isArray(models) ? models : [models]
1186+
if (!modelArray.length)
11851187
return
11861188

1187-
const modelIds = models.map(model => model.id)
1189+
const modelIds = modelArray.map(model => model.id)
11881190

11891191
for (const relation of this.withRelations) {
11901192
const relatedRecords = await DB.instance
@@ -1193,15 +1195,25 @@ export class JobModel {
11931195
.selectAll()
11941196
.execute()
11951197

1196-
models.map((model: JobModel) => {
1198+
if (Array.isArray(models)) {
1199+
// If array, map through all models
1200+
models.map((model: JobModel) => {
1201+
const records = relatedRecords.filter((record: any) => {
1202+
return record.job__id === model.id
1203+
})
1204+
1205+
model[relation] = records.length === 1 ? records[0] : records
1206+
return model
1207+
})
1208+
}
1209+
else {
1210+
// If single model, just filter once
11971211
const records = relatedRecords.filter((record: any) => {
1198-
return record.job_id === model.id
1212+
return record.job__id === models.id
11991213
})
12001214

1201-
model[relation] = records.length === 1 ? records[0] : records
1202-
1203-
return model
1204-
})
1215+
models[relation] = records.length === 1 ? records[0] : records
1216+
}
12051217
}
12061218
}
12071219

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,11 +1250,13 @@ export class PaymentMethodModel {
12501250
}
12511251
}
12521252

1253-
async loadRelations(models: PaymentMethodModel[]): Promise<void> {
1254-
if (!models.length)
1253+
async loadRelations(models: PaymentMethodModel | PaymentMethodModel[]): Promise<void> {
1254+
// Handle both single model and array of models
1255+
const modelArray = Array.isArray(models) ? models : [models]
1256+
if (!modelArray.length)
12551257
return
12561258

1257-
const modelIds = models.map(model => model.id)
1259+
const modelIds = modelArray.map(model => model.id)
12581260

12591261
for (const relation of this.withRelations) {
12601262
const relatedRecords = await DB.instance
@@ -1263,15 +1265,25 @@ export class PaymentMethodModel {
12631265
.selectAll()
12641266
.execute()
12651267

1266-
models.map((model: PaymentMethodModel) => {
1268+
if (Array.isArray(models)) {
1269+
// If array, map through all models
1270+
models.map((model: PaymentMethodModel) => {
1271+
const records = relatedRecords.filter((record: any) => {
1272+
return record.paymentmethod__id === model.id
1273+
})
1274+
1275+
model[relation] = records.length === 1 ? records[0] : records
1276+
return model
1277+
})
1278+
}
1279+
else {
1280+
// If single model, just filter once
12671281
const records = relatedRecords.filter((record: any) => {
1268-
return record.paymentmethod_id === model.id
1282+
return record.paymentmethod__id === models.id
12691283
})
12701284

1271-
model[relation] = records.length === 1 ? records[0] : records
1272-
1273-
return model
1274-
})
1285+
models[relation] = records.length === 1 ? records[0] : records
1286+
}
12751287
}
12761288
}
12771289

0 commit comments

Comments
 (0)