Go Advanced Data Operations
These examples combine the generated School Management APIs into
application-sized patterns. Inspect q.go and each entity's request.go for
the exact names produced by your model.
Search page with rows and two facets
rows, err := lib.Q.Schools().
WithNameContaining("Primary").
SelectSchoolTypeWith(
lib.Q.SchoolTypesMinimal().SelectCode().SelectName()).
SelectPlatformWith(lib.Q.PlatformsMinimal().SelectName()).
FacetBySchoolTypeAs(
"schoolTypes",
lib.Q.SchoolTypesMinimal().
WithCodeIn([]string{"PRIMARY", "SECONDARY"}).
SelectCode().SelectName().CountSchoolsAs("schoolCount"),
true,
).
FacetByPlatformAs(
"platforms",
lib.Q.PlatformsMinimal().SelectName().CountSchoolsAs("schoolCount"),
false,
).
OrderByIdDesc().
Limit(50).
Comment("Search schools with dashboard facets").
Purpose("Render the authorized operations dashboard").
ExecuteForList(context)
if err != nil { return err }
typeBuckets, _ := rows.Facet("schoolTypes")
platformBuckets, _ := rows.Facet("platforms")
The first Facet retains allowed zero-count values. The second is matched-only. Both are sidecars to the primary School list.
Deep graph and per-parent Top-N
platforms, err := lib.Q.Platforms().
SelectName().
SelectSchoolListWith(
lib.Q.Schools().
SelectName().
SelectSchoolTypeWith(
lib.Q.SchoolTypesMinimal().SelectCode().SelectName()).
OrderByStudentCapacityDesc().
Limit(3),
).
Comment("Load each platform and its three largest schools").
Purpose("Render the authorized platform capacity review").
ExecuteForList(context)
if err != nil { return err }
The child limit applies per Platform through the provider's partition/window plan. Do not replace it with an in-memory slice after overfetching children.
Grouped and relation analytics
distribution, err := lib.Q.Schools().
GroupBySchoolType().
CountAs("schoolCount").
SumStudentCapacityAs("capacityTotal").
AvgStudentCapacityAs("capacityAverage").
Comment("Aggregate school capacity by type").
Purpose("Build the authorized capacity report").
ExecuteRecords(context)
if err != nil { return err }
cards, err := lib.Q.SchoolTypesMinimal().
SelectCode().SelectName().
CountSchoolsAs("schoolCount").
SumStudentCapacityOfSchoolsAs("capacityTotal", lib.Q.Schools()).
Comment("Calculate type-level school metrics").
Purpose("Render the authorized type cards").
ExecuteForList(context)
if err != nil { return err }
distribution contains analytic records; cards.Data contains typed parent
entities decorated with named relation metrics.
Audited read-modify-save
school, err := lib.Q.Schools().
WithIdIs(schoolID).
SelectPlatformWith(lib.Q.PlatformsMinimal().SelectName()).
SelectSchoolTypeWith(lib.Q.SchoolTypesMinimal().SelectCode()).
Comment("Load the complete school for capacity approval").
Purpose("Apply an authorized capacity revision").
ExecuteForOne(context)
if err != nil { return err }
if school == nil { return errors.New("school not found") }
school.UpdateStudentCapacity(newCapacity)
school, err = school.AuditAs("Approve revised student capacity").Save(context)
if err != nil { return err }
Load every Checker/Fix input and retain the saved entity so the next mutation uses the authoritative optimistic version.