Skip to main content

View

Trait View 

Source
pub trait View:
    Sized
    + Send
    + Sync
    + 'static {
    type Row: FromRow + Send + Sync + 'static;
    type Select: Query + Send + Sync + 'static;

    // Required method
    fn base_select() -> Self::Select;

    // Provided method
    fn after_select<'a>(
        db: &'a dyn Executor,
        rows: &'a mut Vec<Self::Row>,
    ) -> ExecFuture<'a, Result<(), ExecError>> { ... }
}
Expand description

A readable model: enough to SELECT and map rows. No primary key required — a database view, a reporting projection, a read-only slice of a table are all Views. Table adds the mutations.

Implemented by the generated model marker type (users::Users), not by the row struct: the marker carries the associated types and the hooks, the row struct stays plain data.

§Hooks

after_select is a trait default method — static dispatch, no downcasting, the deliberate departure from bob’s runtime type-assertion opt-in: the generator emits nothing for a model without hooks, an application overrides the method on its model, and the call resolves at compile time. The hook receives &dyn Executor — exactly the executor the caller passed in — so it runs inside the caller’s transaction when there is one, and cannot end a transaction it did not open (the execution layer was shaped for precisely this; see docs/execution.md §Q2).

There is deliberately no before_select: everything a before-select hook could do to the query, a query mod already does at the same call site, and ad-hoc pre-query work rides the QueryExtensions hook channel (ModelSelect::add_hook).

Required Associated Types§

Source

type Row: FromRow + Send + Sync + 'static

The row struct rows decode into, rel field included.

Source

type Select: Query + Send + Sync + 'static

The dialect’s SELECT statement type. Generated models are tied to one dialect here — which is what makes a dialect/backend mismatch a compile-time impossibility rather than a runtime check.

Required Methods§

Source

fn base_select() -> Self::Select

The seeded SELECT: this model’s columns, FROM this model’s table. Everything else — filters, mods, preloads — is applied on top by ModelTable::query.

Provided Methods§

Source

fn after_select<'a>( db: &'a dyn Executor, rows: &'a mut Vec<Self::Row>, ) -> ExecFuture<'a, Result<(), ExecError>>

Runs after rows are mapped and loaders have finished, on the caller’s executor. &mut so a hook may massage the result set.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§