Rust Business Scenarios
Shared query capability
Rust implements the complete seven-runtime native query profile: typed scalar and nested relation predicates, typed relation selection through child Requests, projection and ordering, grouping and portable aggregates, relation statistics, facets, ordinary/continuous/ID-set pagination, and exact per-parent Top-N. See the cross-language query contract for the capability catalog and audited source revision; generated Assist remains the authority for model-specific Rust method names.
The examples use the generated School Management fixture. Read the common data-operations guide first for the distinction between row results, analytics, and Facet sidecars.
Query rows and relations
let schools = Q::schools()
.with_name_containing("Primary")
.select_school_type_with(
Q::school_types_minimal().select_code().select_name())
.order_by_id_desc()
.top(20)
.comment("Find primary schools with their type")
.purpose("Render the authorized school directory")
.execute_for_list(&context)
.await?;
Relation Requests remain typed builders. Put ordering and a bound on a reverse
child Request before passing it to select_..._with(...) for per-parent Top-N.
Aggregate and group
let groups = Q::schools()
.with_name_containing("School")
.group_by_school_type()
.count_as("schoolCount")
.sum_student_capacity_as("capacityTotal")
.avg_student_capacity_as("capacityAverage")
.min_student_capacity_as("capacityMinimum")
.max_student_capacity_as("capacityMaximum")
.comment("Aggregate schools by type")
.purpose("Build the capacity dashboard")
.execute_for_rows(&context)
.await?;
let first_count = groups[0]
.get("schoolCount")
.and_then(|value| value.try_u64());
execute_for_rows preserves group keys and aliases as analytic records;
aliases are not writable entity properties. For
per-parent relation metrics, use the generated relation-stat methods:
let types = Q::school_types_minimal()
.select_code()
.count_schools_as("schoolCount")
.sum_student_capacity_of_schools_as(
"capacityTotal", Q::schools())
.comment("Calculate statistics for every school type")
.purpose("Render type summary cards")
.execute_for_list(&context)
.await?;
Facets
Rust exposes the include-all switch in the generated
..._as_with_options method:
let rows = Q::schools()
.with_name_containing("Primary")
.facet_by_school_type_as_with_options(
"schoolTypes",
Q::school_types_minimal()
.select_code()
.select_name()
.count_schools_as("schoolCount"),
true,
)
.comment("Search schools and calculate type buckets")
.purpose("Render the school search page")
.execute_for_list(&context)
.await?;
let buckets = rows
.facet("schoolTypes")
.expect("SchoolType facet must be present");
let first_count = buckets[0]
.get("schoolCount")
.and_then(|value| value.try_u64());
true preserves allowed zero-count values; false returns only values matched
by the outer filter. The shorter facet_by_school_type_as(...) defaults to
include-all. Restrict the allowed bucket domain on the child Request with, for
example, Q::school_types_minimal().with_code_in(["PRIMARY", "SECONDARY"]).
Multiple Facets can be chained and read independently by name:
let rows = Q::schools()
.facet_by_school_type_as(
"schoolTypes", Q::school_types_minimal().select_code().count_schools_as("count"))
.facet_by_platform_as_with_options(
"platforms", Q::platforms_minimal().select_name().count_schools_as("count"), false)
.comment("Calculate school type and platform facets")
.purpose("Render two authorized filter panels")
.execute_for_list(&context)
.await?;
Create, update, and delete
let mut school = Q::schools()
.comment("Construct a school")
.purpose("Create the approved school record")
.new_entity(&context);
school.update_name("North School");
school.update_student_capacity(600_i64);
school.update_active(true);
let mut school = school
.audit_as("Create North School")
.save(&context)
.await?;
school.update_student_capacity(640_i64);
let mut school = school
.audit_as("Approve revised capacity")
.save(&context)
.await?;
school.mark_for_deletion();
school
.audit_as("Retire the duplicate school record")
.save(&context)
.await?;
Normal updates should begin with a fully loaded current entity so the original optimistic version and Checker inputs are available.
Use these maintained Rust guides for concrete application work:
| Need | Guide |
|---|---|
| Typed queries and generated service shape | Rust Runtime Overview |
| Loaded/null/not-loaded traversal | E Expressions |
| Transaction and graph-write boundaries | Transactions |
| Provider selection and verification | Database Providers |
| Audit event contract | Audit Log Reference |
| AI coding files and generated boundaries | AI Agentic Coding Files |
| Operational practices | Best Practices |
The seven-runtime contract defines shared semantics. Use current generated Assist for model-specific Q and Mutation method spelling.
Continue with Rust advanced data operations for a composed search, relation graph, analytics, Facet, and audited update workflow.