Skip to main content

trigger

Macro trigger 

Source
macro_rules! trigger {
    (
        $trigger_name:ident ($table_name:ident) -> $function_name:ident {
            timing: $timing:ident,
            events: [$($event:ident),* $(,)?],
            orientation: $orientation:ident
            $(, condition: $condition:expr)?
            $(, update_columns: [$($update_col:ident),* $(,)?])?
            $(, composite_fields: [$($composite_field:expr),* $(,)?])?
        }
    ) => { ... };
    (
        $trigger_name:ident ($schema:ident . $table_name:ident) -> $function_schema:ident . $function_name:ident {
            timing: $timing:ident,
            events: [$($event:ident),* $(,)?],
            orientation: $orientation:ident
            $(, condition: $condition:expr)?
            $(, update_columns: [$($update_col:ident),* $(,)?])?
            $(, composite_fields: [$($composite_field:expr),* $(,)?])?
        }
    ) => { ... };
    (@timing before) => { ... };
    (@timing after) => { ... };
    (@timing instead_of) => { ... };
    (@event insert) => { ... };
    (@event update) => { ... };
    (@event delete) => { ... };
    (@event truncate) => { ... };
    (@orientation row) => { ... };
    (@orientation statement) => { ... };
}
Expand description

Macro for defining PostgreSQL triggers

ยงExample

use ignis_db::trigger;

// Simple audit trigger
trigger! {
    audit_users_trigger (users) -> audit_function {
        timing: after,
        events: [insert, update, delete],
        orientation: row
    }
}

// Complex trigger with conditions and composite type support
trigger! {
    validate_address_trigger (users) -> validate_address_func {
        timing: before,
        events: [insert, update],
        orientation: row,
        condition: "NEW.address IS NOT NULL",
        update_columns: [address, phone],
        composite_fields: ["address.street", "address.city"]
    }
}