@@ -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
1920package 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)
2225type 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
104105func 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
114115func 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
0 commit comments