Model

Struct Model 

Source
pub struct Model {
    pub id: ModelId,
    pub table: TableId,
    pub columns: Vec<ColumnId>,
    pub fields: Vec<Field>,
    pub model_to_table: ExprRecord,
    pub table_to_model: TableToModel,
}
Expand description

Defines the bidirectional mapping between a single model and its backing table.

This struct contains the expression templates used during lowering to translate between model-level field references and table-level column references. The mapping supports scenarios where field names differ from column names, where type conversions are required (e.g., Id<T> to String), and where multiple models share a single table.

Fields§

§id: ModelId

The model this mapping applies to.

§table: TableId

The database table that stores this model’s data.

§columns: Vec<ColumnId>

Ordered list of columns that comprise this model’s storage representation.

The order corresponds to the model_to_table expression record: the i-th expression in model_to_table produces the value for the i-th column here.

§fields: Vec<Field>

Per-field mappings.

Indexed by field index within the model. Primitive fields and embedded fields have their respective mappings, while relation fields use Field::Relation since they don’t map directly to columns.

§model_to_table: ExprRecord

Expression template for converting model field values to table column values.

Used during INSERT and UPDATE lowering. Each expression in the record references model fields (via Expr::Reference) and produces a column value. May include type casts (e.g., Id<T> to String) or concatenations for discriminated storage formats.

§table_to_model: TableToModel

Expression template for converting table column values to model field values.

Used during SELECT lowering to construct the RETURNING clause. Each expression references table columns (via Expr::Reference) and produces a model field value. Relation fields are initialized to Null and replaced with subqueries when include() is used.

Implementations§

Source§

impl Model

Source

pub fn resolve_field_mapping(&self, projection: &Projection) -> Option<&Field>

Resolves a projection to the corresponding field mapping.

Handles both single-step projections (primitive/embedded fields) and multi-step projections (nested embedded struct fields). Supports arbitrary nesting depth.

§Examples
  • [2] → field at index 2 (primitive or embedded)
  • [2, 1] → embedded field at index 2, subfield at index 1
  • [2, 1, 0] → nested embedded field at index 2, subfield 1, sub-subfield 0
§Returns

Returns Some(&Field) if the projection is valid. The field can be:

  • Field::Primitive for partial updates to a specific primitive
  • Field::Struct for full replacement of an embedded struct

Returns None if the projection is invalid or points to a relation field.

Trait Implementations§

Source§

impl Clone for Model

Source§

fn clone(&self) -> Model

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Model

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Model

§

impl RefUnwindSafe for Model

§

impl Send for Model

§

impl Sync for Model

§

impl Unpin for Model

§

impl UnwindSafe for Model

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V