Model

Trait Model 

Source
pub trait Model:
    for<'r> FromRow<'r, SqliteRow>
    + Serialize
    + DeserializeOwned
    + Send
    + Sync
    + Sized
    + Unpin
    + ModelValidation
    + ValidationRules {
    // Required methods
    fn table_name() -> &'static str;
    fn id(&self) -> i64;
    fn migrations() -> Vec<Migration>;

    // Provided methods
    fn primary_key() -> &'static str { ... }
    fn register() { ... }
    fn find<'async_trait>(
        id: i64,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
    fn all<'async_trait>(    ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>, DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
    fn create<'async_trait>(
        model: Self,
    ) -> Pin<Box<dyn Future<Output = Result<Self, DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
    fn get_field_value(&self, field: &str) -> Result<i64, DatabaseError> { ... }
    fn create_validated<'async_trait>(
        model: Self,
    ) -> Pin<Box<dyn Future<Output = Result<Self, DatabaseError>> + Send + 'async_trait>>
       where Self: 'async_trait { ... }
}

Required Methods§

Source

fn table_name() -> &'static str

Get the table name for the model

Source

fn id(&self) -> i64

Get the model’s ID

Source

fn migrations() -> Vec<Migration>

Get the migrations for this model

Provided Methods§

Source

fn primary_key() -> &'static str

Get the primary key name (defaults to “id”)

Source

fn register()

Register this model in the registry

Source

fn find<'async_trait>( id: i64, ) -> Pin<Box<dyn Future<Output = Result<Option<Self>, DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Find a model by its primary key

Source

fn all<'async_trait>() -> Pin<Box<dyn Future<Output = Result<Vec<Self>, DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Get all records

Source

fn create<'async_trait>( model: Self, ) -> Pin<Box<dyn Future<Output = Result<Self, DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Create a new record

Source

fn get_field_value(&self, field: &str) -> Result<i64, DatabaseError>

Get a field value by name (used for relationships)

Source

fn create_validated<'async_trait>( model: Self, ) -> Pin<Box<dyn Future<Output = Result<Self, DatabaseError>> + Send + 'async_trait>>
where Self: 'async_trait,

Create a new record with validation

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§