Skip to content

Commit b82339d

Browse files
liangfujianwangzuo
andauthored
Record preload methods fix #18 (#49)
* 实现一对多懒加载 * 测试一对多懒加载 * 实现一对一懒加载 * 测试一对一懒加载 * fmt model.go * ts添加赖加载 * 添加go赖加载测试 * fmt model.gotmpl * fmt model.gotmpl * fmt .tmpl * fmt preload * fmt * fix model preload methods --------- Co-authored-by: Wang Zuo <wzuoadjusted@gmail.com>
1 parent 8ef0c90 commit b82339d

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

generator/client/golang/templates/[model].gotmpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff 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 }}

generator/client/typescript/templates/[model]/[model].tstmpl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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() {

generator/client/typescript/templates/queryx/select.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Code generated by queryx, DO NOT EDIT.
22

3-
import type { Clause } from "./clause";
43
import type { DeleteStatemnet } from "./delete";
54
import { Clause } from "./clause";
65
import { newDelete } from "./delete";

internal/integration/client.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff 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

336344
test("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) {

internal/integration/client_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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

422434
func TestTransaction(t *testing.T) {

0 commit comments

Comments
 (0)