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§
Sourcefn 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 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).
Sourcefn 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 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.
Sourcefn 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 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).
Sourcefn 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 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.
Sourcefn 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 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).
Sourcefn 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 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.
Sourcefn 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 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).
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".