Skip to main content

Trigger

Define after_save Trigger

A common place to trigger notification is right after a database row being inserted or updated. For example in address.rs, we will create a notification after every successful insert or update:

#[async_trait::async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn after_save<C>(model: Model, db: &C, insert: bool) -> Result<Model, DbErr>
where
C: ConnectionTrait,
{
let action = if insert { "Inserted" } else { "Updated" };
let message = format!("{action} Address Model ID: {}", model.address_id);
sea_orm_notify::notify_all(db, &message).await?;
tracing::info!("{message}");
Ok(model)
}
}

Unicast / Multicast

There are two methods to send notification, they are defined in sea-orm-notify/src/lib.rs:

// Send the notification to all webhook
sea_orm_notify::notify_all(db, message).await?;

// Send the notification to a specific webhook only
sea_orm_notify::notify(db, WebhookId(1), message).await?;