Skip to content

Commit

Permalink
Record preload methods fix #18 (#49)
Browse files Browse the repository at this point in the history
* 实现一对多懒加载

* 测试一对多懒加载

* 实现一对一懒加载

* 测试一对一懒加载

* 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 <[email protected]>
  • Loading branch information
liangfujian and wangzuo authored Jan 19, 2024
1 parent 8ef0c90 commit b82339d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
23 changes: 23 additions & 0 deletions generator/client/golang/templates/[model].gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ func ({{ $m }} *{{ $.model.Name }}) Query{{ $h.Name | pascal }}() *{{ $h.ModelNa
return {{ $m }}.queries.Query{{ $h.ModelName }}().Where({{ $m }}.schema.{{ $h.ModelName }}{{ $h.ForeignKey | pascal }}.EQ({{ $m }}.ID))
{{- end }}
}

func ({{ $m }} *{{ $.model.Name }}) Preload{{ $h.Name | pascal }}() error {
records, err := {{ $m }}.Query{{ $h.Name | pascal }}().All()
if err != nil {
return err
}
{{ $m }}.{{ $h.Name | pascal }} = records
return nil
}
{{- end }}

{{- range $h := $.model.HasOne }}

func ({{ $m }} *{{ $.model.Name }}) Preload{{ pascal $h.Name }}() error {
record, err := {{ $m }}.queries.Query{{ $h.ModelName }}().
Where({{ $m }}.schema.{{ pascal $h.Name }}{{ $.model.Name }}ID.EQ({{ $m }}.ID)).
First()
if err != nil {
return err
}
{{ $m }}.{{ pascal $h.Name }} = record
return nil
}
{{- end }}
10 changes: 10 additions & 0 deletions generator/client/typescript/templates/[model]/[model].tstmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ export class {{ $.model.Name }} {
return this._client.query{{ $h.ModelName }}().where(this._client.{{ $h.ModelName | camel }}{{ $h.ForeignKey | pascal }}.eq(this.id));
{{- end }}
}

async preload{{ $h.Name | pascal }}() {
this.{{ $h.Name | camel }} = await this.query{{ $h.Name | pascal }}().all();
}
{{- end }}

{{- range $h := $.model.HasOne }}
async preload{{ $h.ModelName }}() {
this.{{ $h.Name | camel }} = await this._client.query{{ pascal $h.Name }}().where(this._client.{{ $h.Name }}{{ $.model.Name }}ID.eq(this.id)).first();
}
{{- end }}

toString() {
Expand Down
1 change: 0 additions & 1 deletion generator/client/typescript/templates/queryx/select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Code generated by queryx, DO NOT EDIT.

import type { Clause } from "./clause";
import type { DeleteStatemnet } from "./delete";
import { Clause } from "./clause";
import { newDelete } from "./delete";
Expand Down
10 changes: 9 additions & 1 deletion internal/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ test("preload", async () => {
let post = await c.queryPost().preloadUserPosts().find(post1.id);
expect(post.userPosts!.length).toEqual(1);
expect(post.userPosts![0].id).toEqual(userPost1.id);

user = await c.queryUser().find(user1.id);
await user.preloadUserPosts();
expect(user.userPosts!.length).toEqual(2);
await user.preloadPosts();
expect(user.posts!.length).toEqual(2);
await user.preloadAccount();
expect(user.account!.id).toEqual(account1.id);
});

test("transaction", async () => {
Expand Down Expand Up @@ -364,7 +372,7 @@ test("transaction", async () => {
expect(tag1.name).toEqual("tag1-updated");
});

test("transactionBlock", async () => {
test("transaction block", async () => {
await c.queryTag().deleteAll();

await c.transaction(async function (tx: Tx) {
Expand Down
12 changes: 12 additions & 0 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,18 @@ func TestPreload(t *testing.T) {
post, _ := c.QueryPost().PreloadUserPosts().Find(post1.ID)
require.Equal(t, 1, len(post.UserPosts))
require.Equal(t, userPost1.ID, post.UserPosts[0].ID)

user, err := c.QueryUser().Find(user1.ID)
require.NoError(t, err)
err = user.PreloadUserPosts()
require.NoError(t, err)
require.Equal(t, 2, len(user.UserPosts))
err = user.PreloadPosts()
require.NoError(t, err)
require.Equal(t, 2, len(user.Posts))
err = user.PreloadAccount()
require.NoError(t, err)
require.Equal(t, account1.ID, user.Account.ID)
}

func TestTransaction(t *testing.T) {
Expand Down

0 comments on commit b82339d

Please sign in to comment.