File tree Expand file tree Collapse file tree 5 files changed +54
-2
lines changed Expand file tree Collapse file tree 5 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -104,4 +104,27 @@ func ({{ $m }} *{{ $.model.Name }}) Query{{ $h.Name | pascal }}() *{{ $h.ModelNa
104104 return {{ $m }}.queries.Query{{ $h.ModelName }}().Where({{ $m }}.schema.{{ $h.ModelName }}{{ $h.ForeignKey | pascal }}.EQ({{ $m }}.ID))
105105 {{- end }}
106106}
107+
108+ func ({{ $m }} *{{ $.model.Name }}) Preload{{ $h.Name | pascal }}() error {
109+ records, err := {{ $m }}.Query{{ $h.Name | pascal }}().All()
110+ if err != nil {
111+ return err
112+ }
113+ {{ $m }}.{{ $h.Name | pascal }} = records
114+ return nil
115+ }
116+ {{- end }}
117+
118+ {{- range $h := $.model.HasOne }}
119+
120+ func ({{ $m }} *{{ $.model.Name }}) Preload{{ pascal $h.Name }}() error {
121+ record, err := {{ $m }}.queries.Query{{ $h.ModelName }}().
122+ Where({{ $m }}.schema.{{ pascal $h.Name }}{{ $.model.Name }}ID.EQ({{ $m }}.ID)).
123+ First()
124+ if err != nil {
125+ return err
126+ }
127+ {{ $m }}.{{ pascal $h.Name }} = record
128+ return nil
129+ }
107130{{- end }}
Original file line number Diff line number Diff line change @@ -86,6 +86,16 @@ export class {{ $.model.Name }} {
8686 return this._client.query{{ $h.ModelName }}().where(this._client.{{ $h.ModelName | camel }}{{ $h.ForeignKey | pascal }}.eq(this.id));
8787 {{- end }}
8888 }
89+
90+ async preload{{ $h.Name | pascal }}() {
91+ this.{{ $h.Name | camel }} = await this.query{{ $h.Name | pascal }}().all();
92+ }
93+ {{- end }}
94+
95+ {{- range $h := $.model.HasOne }}
96+ async preload{{ $h.ModelName }}() {
97+ this.{{ $h.Name | camel }} = await this._client.query{{ pascal $h.Name }}().where(this._client.{{ $h.Name }}{{ $.model.Name }}ID.eq(this.id)).first();
98+ }
8999 {{- end }}
90100
91101 toString() {
Original file line number Diff line number Diff line change 11// Code generated by queryx, DO NOT EDIT.
22
3- import type { Clause } from "./clause" ;
43import type { DeleteStatemnet } from "./delete" ;
54import { Clause } from "./clause" ;
65import { newDelete } from "./delete" ;
Original file line number Diff line number Diff line change @@ -331,6 +331,14 @@ test("preload", async () => {
331331 let post = await c . queryPost ( ) . preloadUserPosts ( ) . find ( post1 . id ) ;
332332 expect ( post . userPosts ! . length ) . toEqual ( 1 ) ;
333333 expect ( post . userPosts ! [ 0 ] . id ) . toEqual ( userPost1 . id ) ;
334+
335+ user = await c . queryUser ( ) . find ( user1 . id ) ;
336+ await user . preloadUserPosts ( ) ;
337+ expect ( user . userPosts ! . length ) . toEqual ( 2 ) ;
338+ await user . preloadPosts ( ) ;
339+ expect ( user . posts ! . length ) . toEqual ( 2 ) ;
340+ await user . preloadAccount ( ) ;
341+ expect ( user . account ! . id ) . toEqual ( account1 . id ) ;
334342} ) ;
335343
336344test ( "transaction" , async ( ) => {
@@ -364,7 +372,7 @@ test("transaction", async () => {
364372 expect ( tag1 . name ) . toEqual ( "tag1-updated" ) ;
365373} ) ;
366374
367- test ( "transactionBlock " , async ( ) => {
375+ test ( "transaction block " , async ( ) => {
368376 await c . queryTag ( ) . deleteAll ( ) ;
369377
370378 await c . transaction ( async function ( tx : Tx ) {
Original file line number Diff line number Diff line change @@ -417,6 +417,18 @@ func TestPreload(t *testing.T) {
417417 post , _ := c .QueryPost ().PreloadUserPosts ().Find (post1 .ID )
418418 require .Equal (t , 1 , len (post .UserPosts ))
419419 require .Equal (t , userPost1 .ID , post .UserPosts [0 ].ID )
420+
421+ user , err := c .QueryUser ().Find (user1 .ID )
422+ require .NoError (t , err )
423+ err = user .PreloadUserPosts ()
424+ require .NoError (t , err )
425+ require .Equal (t , 2 , len (user .UserPosts ))
426+ err = user .PreloadPosts ()
427+ require .NoError (t , err )
428+ require .Equal (t , 2 , len (user .Posts ))
429+ err = user .PreloadAccount ()
430+ require .NoError (t , err )
431+ require .Equal (t , account1 .ID , user .Account .ID )
420432}
421433
422434func TestTransaction (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments