@@ -6,29 +6,23 @@ import type {
6
6
import { camelCase , pascalCase , plural , singular , snakeCase } from '@stacksjs/strings'
7
7
import { fetchOtherModelRelations , getFillableAttributes , getGuardedAttributes , getHiddenAttributes , getRelationCount , getRelations , getRelationType , mapEntity } from './utils'
8
8
9
- function generateCustomAccessors ( model : Model ) : { output : string , loopString : string , loopStringPlural : string } {
9
+ function generateCustomAccessors ( model : Model ) : { output : string , loopString : string } {
10
10
let output = ''
11
11
let loopString = ''
12
- let loopStringPlural = ''
13
12
14
13
if ( model . get ) {
15
14
for ( const [ methodName , getter ] of Object . entries ( model . get ) ) {
16
15
const getterStr = getter . toString ( )
17
- output += `${ methodName } : ${ getterStr } , \n`
16
+ output += removeAttrString ( `${ methodName } : ${ getterStr } , \n` )
18
17
}
19
18
20
19
loopString += `
21
20
for (const [key, fn] of Object.entries(customGetter)) {
22
21
model[key] = fn()
23
22
}`
24
-
25
- loopStringPlural += `
26
- for (const [key, fn] of Object.entries(customGetter)) {
27
- models[key] = fn()
28
- }`
29
23
}
30
24
31
- return { output, loopString, loopStringPlural }
25
+ return { output, loopString }
32
26
}
33
27
34
28
function removeAttrString ( getterFn : string ) : string {
@@ -910,22 +904,24 @@ export async function generateModelString(
910
904
}
911
905
912
906
mapCustomGetters(models: ${ modelName } JsonResponse | ${ modelName } JsonResponse[]): void {
913
- if (Array.isArray(models)) {
914
- models.map((model: ${ modelName } JsonResponse) => {
915
- const customGetter = {
916
- ${ removeAttrString ( getterOutput . output ) }
917
- }
907
+ const data = models
908
+
909
+ const customGetter = {
910
+ ${ getterOutput . output }
911
+ }
912
+
913
+ if (Array.isArray(data)) {
914
+ data.map((model: ${ modelName } JsonResponse) => {
915
+
918
916
919
917
${ getterOutput . loopString }
920
918
921
919
return model
922
920
})
923
921
} else {
924
- const customGetter = {
925
- ${ removeAttrString ( getterOutput . output ) }
926
- }
922
+ const model = data
927
923
928
- ${ getterOutput . loopStringPlural }
924
+ ${ getterOutput . loopString }
929
925
}
930
926
}
931
927
@@ -998,7 +994,7 @@ export async function generateModelString(
998
994
if (!model)
999
995
return undefined
1000
996
1001
- await this.mapCustomGetters(model)
997
+ this.mapCustomGetters(model)
1002
998
await this.loadRelations(model)
1003
999
1004
1000
const data = new ${ modelName } Model(model as ${ modelName } Type)
@@ -1030,7 +1026,7 @@ export async function generateModelString(
1030
1026
}
1031
1027
1032
1028
if (model) {
1033
- await this.mapCustomGetters(model)
1029
+ this.mapCustomGetters(model)
1034
1030
await this.loadRelations(model)
1035
1031
}
1036
1032
@@ -1046,7 +1042,7 @@ export async function generateModelString(
1046
1042
.selectAll()
1047
1043
.executeTakeFirst()
1048
1044
1049
- await instance.mapCustomGetters(model)
1045
+ instance.mapCustomGetters(model)
1050
1046
1051
1047
const data = new ${ modelName } Model(model as ${ modelName } Type)
1052
1048
@@ -1060,7 +1056,7 @@ export async function generateModelString(
1060
1056
throw new ModelNotFoundException(404, 'No ${ modelName } Model results found for query')
1061
1057
1062
1058
if (model) {
1063
- await this.mapCustomGetters(model)
1059
+ this.mapCustomGetters(model)
1064
1060
await this.loadRelations(model)
1065
1061
}
1066
1062
@@ -1084,7 +1080,7 @@ export async function generateModelString(
1084
1080
1085
1081
const models = await DB.instance.selectFrom('${ tableName } ').selectAll().execute()
1086
1082
1087
- await instance.mapCustomGetters(model)
1083
+ instance.mapCustomGetters(model)
1088
1084
1089
1085
const data = await Promise.all(models.map(async (model: ${ modelName } Type) => {
1090
1086
return new ${ modelName } Model(model)
@@ -1103,7 +1099,7 @@ export async function generateModelString(
1103
1099
1104
1100
cache.getOrSet(\`${ formattedModelName } :\${id}\`, JSON.stringify(model))
1105
1101
1106
- await this.mapCustomGetters(model)
1102
+ this.mapCustomGetters(model)
1107
1103
await this.loadRelations(model)
1108
1104
1109
1105
const data = new ${ modelName } Model(model as ${ modelName } Type)
@@ -1132,7 +1128,7 @@ export async function generateModelString(
1132
1128
1133
1129
const models = await query.execute()
1134
1130
1135
- await instance.mapCustomGetters(models)
1131
+ instance.mapCustomGetters(models)
1136
1132
await instance.loadRelations(models)
1137
1133
1138
1134
return models.map((modelItem: ${ modelName } Model) => instance.parseResult(new ${ modelName } Model(modelItem)))
@@ -1328,7 +1324,7 @@ export async function generateModelString(
1328
1324
models = await this.selectFromQuery.selectAll().execute()
1329
1325
}
1330
1326
1331
- await this.mapCustomGetters(model )
1327
+ this.mapCustomGetters(models )
1332
1328
await this.loadRelations(models)
1333
1329
1334
1330
const data = await Promise.all(models.map(async (model: ${ modelName } Model) => {
@@ -1911,7 +1907,7 @@ export async function generateModelString(
1911
1907
if (!model)
1912
1908
return undefined
1913
1909
1914
- await instance.mapCustomGetters(model)
1910
+ instance.mapCustomGetters(model)
1915
1911
1916
1912
const data = new ${ modelName } Model(model as ${ modelName } Type)
1917
1913
@@ -1929,7 +1925,7 @@ export async function generateModelString(
1929
1925
if (!model)
1930
1926
return undefined
1931
1927
1932
- await instance.mapCustomGetters(model)
1928
+ instance.mapCustomGetters(model)
1933
1929
1934
1930
const data = new ${ modelName } Model(model as ${ modelName } Type)
1935
1931
@@ -1957,7 +1953,7 @@ export async function generateModelString(
1957
1953
.executeTakeFirst()
1958
1954
1959
1955
if (existing${ modelName } ) {
1960
- await instance.mapCustomGetters(model)
1956
+ instance.mapCustomGetters(model)
1961
1957
await instance.loadRelations(model)
1962
1958
1963
1959
return new ${ modelName } Model(existing${ modelName } as ${ modelName } Type)
@@ -2071,7 +2067,7 @@ export async function generateModelString(
2071
2067
}
2072
2068
2073
2069
if (model) {
2074
- await this.mapCustomGetters(model)
2070
+ this.mapCustomGetters(model)
2075
2071
await this.loadRelations(model)
2076
2072
}
2077
2073
0 commit comments