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,
pub default_returning: Expr,
}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.
§Examples
use toasty_core::schema::mapping::Model;
let model_mapping: &Model = schema.mapping_for(model_id);
println!("model {:?} -> table {:?}", model_mapping.id, model_mapping.table);
println!("{} columns, {} fields", model_mapping.columns.len(), model_mapping.fields.len());Fields§
§id: ModelIdThe model this mapping applies to.
table: TableIdThe 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: ExprRecordExpression 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: TableToModelExpression 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.
default_returning: ExprPre-computed default RETURNING expression for this model.
Same shape as table_to_model’s record, but with every #[deferred]
field — at this level or nested inside an embedded type — pre-masked
to Null. Lowering starts from this expression and splices loaded
forms in for fields named by .include() (or for every deferred
field when an INSERT ... RETURNING is being lowered).
Implementations§
Source§impl Model
impl Model
Sourcepub fn resolve_field_mapping(&self, projection: &Projection) -> Option<&Field>
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::Primitivefor partial updates to a specific primitiveField::Structfor full replacement of an embedded struct
Returns None if the projection is invalid or points to a relation field.