@@ -29,6 +29,25 @@ function generateCustomAccessors(model: Model): { output: string, loopString: st
29
29
return { output, loopString }
30
30
}
31
31
32
+ function generateCustomSetters ( model : Model ) : { output : string , loopString : string } {
33
+ let output = ''
34
+ let loopString = ''
35
+
36
+ if ( model . set ) {
37
+ for ( const [ methodName , getter ] of Object . entries ( model . set ) ) {
38
+ const getterStr = getter . toString ( )
39
+ output += removeAttrString ( `${ methodName } : ${ getterStr } , \n` )
40
+ }
41
+
42
+ loopString += `
43
+ for (const [key, fn] of Object.entries(customGetter)) {
44
+ model[key] = fn()
45
+ }`
46
+ }
47
+
48
+ return { output, loopString }
49
+ }
50
+
32
51
function removeAttrString ( getterFn : string ) : string {
33
52
return getterFn . replace ( '(attributes)' , '()' ) . replace ( 'attributes' , 'model' )
34
53
}
@@ -97,6 +116,7 @@ export async function generateModelString(
97
116
let privateSoftDeletes = ''
98
117
99
118
const getterOutput = await generateCustomAccessors ( model )
119
+ const setterOutput = await generateCustomSetters ( model )
100
120
101
121
const relations = await getRelations ( model , modelName )
102
122
@@ -932,6 +952,31 @@ export async function generateModelString(
932
952
}
933
953
}
934
954
955
+ mapCustomSetters(models: ${ modelName } JsonResponse | ${ modelName } JsonResponse[]): void {
956
+ const data = models
957
+
958
+ if (Array.isArray(data)) {
959
+ data.map((model: ${ modelName } JsonResponse) => {
960
+
961
+ const customGetter = {
962
+ ${ setterOutput . output }
963
+ }
964
+
965
+ ${ setterOutput . loopString }
966
+
967
+ return model
968
+ })
969
+ } else {
970
+ const model = data
971
+
972
+ const customGetter = {
973
+ ${ setterOutput . output }
974
+ }
975
+
976
+ ${ setterOutput . loopString }
977
+ }
978
+ }
979
+
935
980
${ getFields }
936
981
${ setFields }
937
982
0 commit comments