Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync {
    type Schema;
    type FieldChanges: Send + Sync;

    // Required methods
    fn table_name() -> &'static str;
    fn schema_version() -> &'static str;
    fn get<'life0, 'life1, 'async_trait>(
        id: &'life0 str,
        valence: &'life1 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn create<'life0, 'async_trait>(
        data: Self,
        valence: &'life0 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
    fn update<'life0, 'life1, 'async_trait>(
        id: &'life0 str,
        data: Self,
        valence: &'life1 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        id: &'life0 str,
        valence: &'life1 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait;
    fn upsert<'life0, 'life1, 'async_trait>(
        id: &'life0 str,
        data: Self,
        valence: &'life1 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn merge<'life0, 'life1, 'async_trait>(
        id: &'life0 str,
        patch: Value,
        valence: &'life1 Valence,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
}
Expand description

Core trait that all generated models implement.

CRUD methods route through the active Valence backend, applying privacy and ownership hooks defined in the source schema.

§Examples

Generated models (from valence-codegen) implement this trait. After including $OUT_DIR/generated_models.rs:

use valence::Model;

let created = Widget::create(widget, &valence).await?;
let loaded = Widget::get(created.id(), &valence).await?;
Widget::update(created.id(), updated, &valence).await?;
Widget::delete(created.id(), &valence).await?;

See workspace examples/codegen-host and examples/product-model-host.

Required Associated Types§

Source

type Schema

Generated schema metadata type for this model.

Source

type FieldChanges: Send + Sync

Field-level change set type used by update/merge paths.

Required Methods§

Source

fn table_name() -> &'static str

Physical table name from the schema DSL table: key.

Source

fn schema_version() -> &'static str

Schema version string from the DSL version: key.

Source

fn get<'life0, 'life1, 'async_trait>( id: &'life0 str, valence: &'life1 Valence, ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Fetch one row by primary key; returns Ok(None) when absent.

Source

fn create<'life0, 'async_trait>( data: Self, valence: &'life0 Valence, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

Insert a new row.

Source

fn update<'life0, 'life1, 'async_trait>( id: &'life0 str, data: Self, valence: &'life1 Valence, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Replace an existing row by id.

Source

fn delete<'life0, 'life1, 'async_trait>( id: &'life0 str, valence: &'life1 Valence, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait,

Delete one row by id.

Source

fn upsert<'life0, 'life1, 'async_trait>( id: &'life0 str, data: Self, valence: &'life1 Valence, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Create or replace a row by explicit id.

Source

fn merge<'life0, 'life1, 'async_trait>( id: &'life0 str, patch: Value, valence: &'life1 Valence, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Patch an existing row with a partial JSON object when the backend supports merge.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§