Skip to main content

ModelLifecycle

Trait ModelLifecycle 

Source
pub trait ModelLifecycle:
    Sized
    + Send
    + Sync {
    // Provided methods
    fn saving<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn saved<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn creating<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn created<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn updating<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn updated<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn deleting<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
    fn deleted<'life0, 'life1, 'async_trait>(
        &'life0 self,
        _pool: &'life1 DatabasePool,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait { ... }
}
Expand description

Trait untuk siklus hidup model (Model Lifecycle Events)

Memungkinkan pengeksekusian logika otomatis (side-effects) sebelum atau sesudah data disimpan (INSERT/UPDATE) atau dihapus (DELETE).

§Contoh Penggunaan:

ⓘ
#[async_trait::async_trait]
impl ModelLifecycle for Article {
    async fn creating(&mut self, pool: &DatabasePool) -> Result<(), sqlx::Error> {
        self.slug = AutoSlug::generate(pool, "articles", &self.title, None).await?;
        Ok(())
    }
}

Provided Methods§

Source

fn saving<'life0, 'life1, 'async_trait>( &'life0 mut self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan sebelum data disimpan (baik operasi INSERT maupun UPDATE).

Source

fn saved<'life0, 'life1, 'async_trait>( &'life0 self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan setelah data berhasil disimpan ke database.

Source

fn creating<'life0, 'life1, 'async_trait>( &'life0 mut self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan sebelum data baru di-insert ke database (id == 0).

Source

fn created<'life0, 'life1, 'async_trait>( &'life0 self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan setelah data baru berhasil di-insert ke database.

Source

fn updating<'life0, 'life1, 'async_trait>( &'life0 mut self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan sebelum data lama diperbarui (id > 0).

Source

fn updated<'life0, 'life1, 'async_trait>( &'life0 self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan setelah data lama berhasil diperbarui di database.

Source

fn deleting<'life0, 'life1, 'async_trait>( &'life0 self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan sebelum data dihapus (baik soft-delete maupun hard-delete).

Source

fn deleted<'life0, 'life1, 'async_trait>( &'life0 self, _pool: &'life1 DatabasePool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

Berjalan setelah data berhasil dihapus dari database.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§