Skip to content

Commit bdb24e2

Browse files
committed
Support new table_name tag and change schema definition to an anonymous struct
1 parent 0ce7f21 commit bdb24e2

File tree

19 files changed

+151
-95
lines changed

19 files changed

+151
-95
lines changed

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ This is a code generator to generate a bunch of structs and functions to impleme
1515
**Your custom type defines fields for reading and writing**
1616

1717
*model/customer.go*
18+
1819
```go
1920
package model
2021

22+
import "github.com/networkteam/construct/v2"
23+
2124
// Customer is just a struct, only struct tags are used in construct to generate code (no struct embedding needed)
2225
type Customer struct {
23-
ID uuid.UUID `read_col:"customers.customer_id" write_col:"customer_id"`
24-
Name string `read_col:"customers.name,sortable" write_col:"name"`
25-
// Fields can be serialized as JSON by adding a "json" option to the "write_col" tag.
26-
// It works perfectly with a column of type jsonb or json in PostgreSQL.
27-
ContactPerson Contact `read_col:"customers.contact_person,sortable" write_col:"contact_person,json"`
26+
ID uuid.UUID `table_name:"customers" read_col:"customers.customer_id" write_col:"customer_id"` // The table_name tag is optional and can be specified at most once per struct
27+
Name string `read_col:"customers.name,sortable" write_col:"name"`
28+
// Fields can be serialized as JSON by adding a "json" option to the "write_col" tag.
29+
// It works perfectly with a column of type jsonb or json in PostgreSQL.
30+
ContactPerson Contact `read_col:"customers.contact_person,sortable" write_col:"contact_person,json"`
2831

2932
// DomainCount is not mapped to the table but used in the select for reading an aggregate count
3033
DomainCount int
@@ -79,31 +82,29 @@ import (
7982
"github.com/networkteam/qrb/fn"
8083
"github.com/networkteam/qrb/qrbpgx"
8184

82-
".../myproject/domain"
85+
".../myproject/model"
8386
)
8487

85-
var customers = qrb.N("customers")
86-
87-
func FindCustomerByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (domain.Customer, error) {
88+
func FindCustomerByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (model.Customer, error) {
8889
q := qrb.
8990
Select(customerJson()).
90-
From(customers).
91+
From(customer).
9192
// constants for read columns are generated as qrb identifier expressions by construct
92-
LeftJoin(domains).On(domain_customerID.Eq(customer_id)).
93-
Where(customer_id.Eq(qrb.Arg(id))).
94-
GroupBy(customer_id)
93+
LeftJoin(domain).On(domain.customerID.Eq(customer.id)).
94+
Where(customer.id.Eq(qrb.Arg(id))).
95+
GroupBy(customer.id)
9596

9697
row, err := qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx)
9798
if err != nil {
9899
return result, err
99100
}
100-
return pgxScanRow[domain.Customer](row)
101+
return pgxScanRow[model.Customer](row)
101102
}
102103

103104
// CustomerChangeSet is generated by construct for handling partially filled models
104105
func InsertCustomer(ctx context.Context, executor qrbpgx.Executor, changeSet CustomerChangeSet) error {
105106
q := qrb.
106-
InsertInto(customers).
107+
InsertInto(customer).
107108
// toMap is generated by construct
108109
SetMap(changeSet.toMap())
109110

@@ -113,8 +114,8 @@ func InsertCustomer(ctx context.Context, executor qrbpgx.Executor, changeSet Cus
113114

114115
func UpdateCustomer(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID, changeSet CustomerChangeSet) error {
115116
q := qrb.
116-
Update(customers).
117-
Where(customer_id.Eq(qrb.Arg(id))).
117+
Update(customer).
118+
Where(customer.id.Eq(qrb.Arg(id))).
118119
SetMap(changeSet.toMap())
119120

120121
res, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)
@@ -129,7 +130,7 @@ func customerJson() builder.JsonBuildObjectBuilder {
129130
// customerDefaultJson is generated by construct and is a JsonBuildObjectBuilder that can be further modified (immutable)
130131
return customerDefaultJson.
131132
// It's easy to set additional properties
132-
Prop("DomainCount", qrb.Count(domain_id))
133+
Prop("DomainCount", qrb.Count(domain.id))
133134
}
134135
```
135136

example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/gofrs/uuid v4.4.0+incompatible
77
github.com/jackc/pgx/v5 v5.3.1
88
github.com/networkteam/construct/v2 v2.0.0
9-
github.com/networkteam/qrb v0.3.0
9+
github.com/networkteam/qrb v0.3.2
1010
github.com/olekukonko/tablewriter v0.0.5
1111
github.com/urfave/cli/v2 v2.25.1
1212
)

example/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ github.com/jackc/puddle/v2 v2.2.0 h1:RdcDk92EJBuBS55nQMMYFXTxwstHug4jkhT5pq8VxPk
1818
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
1919
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
2020
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
21-
github.com/networkteam/qrb v0.3.0 h1:ISKHpdFsgSTVaD1u0IJGVAVimkkUnRnsY5u3HFFSzWM=
22-
github.com/networkteam/qrb v0.3.0/go.mod h1:rRD44E0t1wI+YHLOH/CPUIbYx/dKgetNYkFXVixDPfc=
21+
github.com/networkteam/qrb v0.3.2 h1:+RiQR/ZC7/JXA9MllFU9ZN/Z7Cd/sLlKfFNzM+J2rT4=
22+
github.com/networkteam/qrb v0.3.2/go.mod h1:rRD44E0t1wI+YHLOH/CPUIbYx/dKgetNYkFXVixDPfc=
2323
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
2424
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
2525
github.com/pashagolub/pgxmock/v2 v2.6.0 h1:Dk07FlzdMOrqdxrelmEeWP9uqcDm757HZWrIwyvdPsE=

example/pgx/model/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
// Project model with todos
88
type Project struct {
9-
ID uuid.UUID `read_col:"projects.id" write_col:"id"`
9+
ID uuid.UUID `table_name:"projects" read_col:"projects.id" write_col:"id"`
1010
Title string `read_col:"projects.title" write_col:"title"`
1111

1212
// for embedded loading of counts

example/pgx/model/todo.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import (
44
"time"
55

66
"github.com/gofrs/uuid"
7+
8+
"github.com/networkteam/construct/v2"
79
)
810

911
// Todo model belonging to a project
1012
type Todo struct {
11-
ID uuid.UUID `read_col:"todos.id" write_col:"id"`
12-
ProjectID uuid.UUID `read_col:"todos.project_id" write_col:"project_id"`
13-
Title string `read_col:"todos.title" write_col:"title"`
14-
CompletedAt *time.Time `read_col:"todos.completed_at" write_col:"completed_at"`
13+
construct.Table `table_name:"todos"` // The table_name struct tag could also be applied to an empty embedded struct
14+
ID uuid.UUID `read_col:"todos.id" write_col:"id"`
15+
ProjectID uuid.UUID `read_col:"todos.project_id" write_col:"project_id"`
16+
Title string `read_col:"todos.title" write_col:"title"`
17+
CompletedAt *time.Time `read_col:"todos.completed_at" write_col:"completed_at"`
1518

1619
// for eager loading of referenced records
1720
Project *Project

example/pgx/repository/mappings_project_gen.go

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pgx/repository/mappings_todo_gen.go

Lines changed: 18 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/pgx/repository/repository_project.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,25 @@ import (
1313
"github.com/networkteam/construct/v2/example/pgx/model"
1414
)
1515

16-
var projects = N("projects")
17-
1816
// projectBuildFindQuery creates a partial builder.SelectBuilder that
1917
// - selects a single JSON result by using buildProjectJson
2018
// - from the projects table
2119
// - and left joins an aggregation of todo counts by project
2220
func projectBuildFindQuery() builder.SelectBuilder {
2321
return SelectJson(projectJson()).
24-
From(projects).
22+
From(project).
2523
LeftJoin(
26-
Select(fn.Count(todo_projectID)).As("count").
27-
Select(todo_projectID).
28-
From(todos).
29-
GroupBy(todo_projectID),
30-
).As("todo_counts").On(project_id.Eq(N("todo_counts.project_id")))
24+
Select(fn.Count(todo.projectID)).As("count").
25+
Select(todo.projectID).
26+
From(todo).
27+
GroupBy(todo.projectID),
28+
).As("todo_counts").On(project.id.Eq(N("todo_counts.project_id")))
3129
}
3230

3331
// FindProjectByID finds a single project by id
3432
func FindProjectByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (result model.Project, err error) {
3533
q := projectBuildFindQuery().
36-
Where(project_id.Eq(Arg(id)))
34+
Where(project.id.Eq(Arg(id)))
3735

3836
row, err := qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx)
3937
if err != nil {
@@ -45,7 +43,7 @@ func FindProjectByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID
4543
// FindAllProjects finds all projects sorted by title
4644
func FindAllProjects(ctx context.Context, executor qrbpgx.Executor) (result []model.Project, err error) {
4745
q := projectBuildFindQuery().
48-
OrderBy(project_title)
46+
OrderBy(project.title)
4947

5048
rows, err := qrbpgx.Build(q).WithExecutor(executor).Query(ctx)
5149
if err != nil {
@@ -57,7 +55,7 @@ func FindAllProjects(ctx context.Context, executor qrbpgx.Executor) (result []mo
5755

5856
// InsertProject inserts a new project from a ProjectChangeSet
5957
func InsertProject(ctx context.Context, executor qrbpgx.Executor, changeSet ProjectChangeSet) error {
60-
q := InsertInto(projects).
58+
q := InsertInto(project).
6159
SetMap(changeSet.toMap())
6260

6361
_, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)

example/pgx/repository/repository_todo.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,21 @@ import (
1212
"github.com/networkteam/construct/v2/example/pgx/model"
1313
)
1414

15-
var todos = qrb.N("todos")
16-
1715
// todoBuildFindQuery creates a partial builder.SelectBuilder that
1816
// - selects a single JSON result by using todoJson()
1917
// - from the todos table
2018
// - and left join the projects for eagerly fetching the project for each todo in a single query
2119
func todoBuildFindQuery() builder.SelectBuilder {
2220
return qrb.
2321
SelectJson(todoJson()).
24-
From(todos).
25-
LeftJoin(projects).On(todo_projectID.Eq(project_id))
22+
From(todo).
23+
LeftJoin(project).On(todo.projectID.Eq(project.id))
2624
}
2725

2826
// FindTodoByID finds a single todo by id
2927
func FindTodoByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (result model.Todo, err error) {
3028
q := todoBuildFindQuery().
31-
Where(todo_id.Eq(qrb.Arg(id)))
29+
Where(todo.id.Eq(qrb.Arg(id)))
3230

3331
row, err := qrbpgx.Build(q).WithExecutor(executor).QueryRow(ctx)
3432
if err != nil {
@@ -40,7 +38,7 @@ func FindTodoByID(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID) (
4038
// FindAllTodos finds all todos sorted by title
4139
func FindAllTodos(ctx context.Context, executor qrbpgx.Executor, filter model.TodosFilter) (result []model.Todo, err error) {
4240
q := todoBuildFindQuery().
43-
OrderBy(todo_completedAt).Desc().NullsFirst()
41+
OrderBy(todo.completedAt).Desc().NullsFirst()
4442

4543
rows, err := qrbpgx.Build(q).WithExecutor(executor).Query(ctx)
4644
if err != nil {
@@ -53,7 +51,7 @@ func FindAllTodos(ctx context.Context, executor qrbpgx.Executor, filter model.To
5351
// InsertTodo inserts a new todo with values from changeSet
5452
func InsertTodo(ctx context.Context, executor qrbpgx.Executor, changeSet TodoChangeSet) error {
5553
q := qrb.
56-
InsertInto(todos).
54+
InsertInto(todo).
5755
SetMap(changeSet.toMap())
5856

5957
_, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)
@@ -63,9 +61,9 @@ func InsertTodo(ctx context.Context, executor qrbpgx.Executor, changeSet TodoCha
6361
// UpdateTodo updates a todo with the given id and changes from changeSet
6462
func UpdateTodo(ctx context.Context, executor qrbpgx.Executor, id uuid.UUID, changeSet TodoChangeSet) error {
6563
q := qrb.
66-
Update(todos).
64+
Update(todo).
6765
SetMap(changeSet.toMap()).
68-
Where(todo_id.Eq(qrb.Arg(id)))
66+
Where(todo.id.Eq(qrb.Arg(id)))
6967

7068
res, err := qrbpgx.Build(q).WithExecutor(executor).Exec(ctx)
7169
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dave/jennifer v1.6.0
77
github.com/fatih/structtag v1.2.0
88
github.com/gofrs/uuid v4.4.0+incompatible
9-
github.com/networkteam/qrb v0.3.0
9+
github.com/networkteam/qrb v0.3.2
1010
github.com/stretchr/testify v1.8.2
1111
github.com/urfave/cli/v2 v2.25.1
1212
golang.org/x/tools v0.7.0

0 commit comments

Comments
 (0)