Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + Unpin
    + for<'r> FromRow<'r, AnyRow> {
    // Required methods
    fn table_name() -> &'static str;
    fn fields() -> &'static [&'static str];
    fn create<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        db: &'life1 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn update<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        db: &'life1 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        db: &'life1 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn force_delete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        db: &'life1 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn has_soft_delete() -> bool { ... }
    fn find<'life0, 'async_trait>(
        db: &'life0 (impl 'async_trait + Database),
        id: i64,
    ) -> Pin<Box<dyn Future<Output = Result<Option<Self>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn all<'life0, 'async_trait>(
        db: &'life0 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn validate(&self) -> Result<(), String> { ... }
    fn save<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        db: &'life1 (impl 'async_trait + Database),
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Model trait for database entities

Required Methods§

Source

fn table_name() -> &'static str

Get the table name

Source

fn fields() -> &'static [&'static str]

Get the list of fields (columns)

Source

fn create<'life0, 'life1, 'async_trait>( &'life0 mut self, db: &'life1 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Create a new record

Source

fn update<'life0, 'life1, 'async_trait>( &'life0 mut self, db: &'life1 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Update an existing record

Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 self, db: &'life1 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete the record (soft delete if supported, otherwise hard delete)

Source

fn force_delete<'life0, 'life1, 'async_trait>( &'life0 self, db: &'life1 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Force delete the record (hard delete)

Provided Methods§

Source

fn has_soft_delete() -> bool

Check if the model supports soft deletes

Source

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

Find a record by ID

Source

fn all<'life0, 'async_trait>( db: &'life0 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<Vec<Self>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Find all records

Source

fn validate(&self) -> Result<(), String>

Validate the model fields

Source

fn save<'life0, 'life1, 'async_trait>( &'life0 mut self, db: &'life1 (impl 'async_trait + Database), ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Save (create or update)

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§