-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db521d3
commit fac1fa9
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//go:build tutorial_4 | ||
// +build tutorial_4 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/ditrit/badaas-orm-tutorial/conditions" | ||
"github.com/ditrit/badaas-orm-tutorial/models" | ||
"github.com/ditrit/badaas/orm" | ||
"github.com/ditrit/badaas/orm/model" | ||
"go.uber.org/fx" | ||
) | ||
|
||
// Target: get all cities whose name is 'Paris' and preload its country | ||
func tutorial(crudCityService orm.CRUDService[models.City, model.UUID], shutdowner fx.Shutdowner) { | ||
cities, err := crudCityService.Query( | ||
conditions.CityName(orm.Eq("Paris")), | ||
conditions.CityPreloadCountry, | ||
) | ||
|
||
// Equivalent to: | ||
// cities, err := crudCityService.Query( | ||
// conditions.CityName(orm.Eq("Paris")), | ||
// conditions.CityCountry( | ||
// conditions.CountryPreloadAttributes, | ||
// ), | ||
// ) | ||
|
||
// SQL executed: | ||
// SELECT cities.*, | ||
// Country.id AS Country__id,Country.created_at AS Country__created_at,Country.updated_at AS Country__updated_at,Country.deleted_at AS Country__deleted_at,Country.name AS Country__name,Country.capital_id AS Country__capital_id | ||
// FROM cities | ||
// LEFT JOIN countries Country ON | ||
// Country.id = cities.country_id AND Country.deleted_at IS NULL | ||
// WHERE cities.name = "Paris" AND cities.deleted_at IS NULL | ||
|
||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
log.Println("Cities named 'Paris' are:") | ||
for i, city := range cities { | ||
fmt.Printf("\t%v: %+v with country: ", i+1, city) | ||
|
||
cityCountry, err := city.GetCountry() | ||
if err != nil { | ||
log.Panicln(err) | ||
} | ||
|
||
fmt.Printf("%+v\n", cityCountry) | ||
} | ||
|
||
shutdowner.Shutdown() | ||
} |