Fire BEFORE ROW triggers for an UPDATE operation. Returns the (possibly modified) NEW fields. `old_fields` is the row before the update (read-only in the trigger body as OLD.*). If a trigger raises an exception, the UPDATE is aborted.
(
state: &SharedState,
identity: &AuthenticatedIdentity,
tenant_id: TenantId,
collection: &str,
old_fields: &HashMap<String, nodedb_types::Value>,
new_fields: &HashMap<String,
| 77 | /// |
| 78 | /// If a trigger raises an exception, the UPDATE is aborted. |
| 79 | pub async fn fire_before_update( |
| 80 | state: &SharedState, |
| 81 | identity: &AuthenticatedIdentity, |
| 82 | tenant_id: TenantId, |
| 83 | collection: &str, |
| 84 | old_fields: &HashMap<String, nodedb_types::Value>, |
| 85 | new_fields: &HashMap<String, nodedb_types::Value>, |
| 86 | cascade_depth: u32, |
| 87 | ) -> crate::Result<HashMap<String, nodedb_types::Value>> { |
| 88 | let triggers = |
| 89 | state |
| 90 | .trigger_registry |
| 91 | .get_matching(tenant_id.as_u64(), collection, DmlEvent::Update); |
| 92 | |
| 93 | let before_triggers: Vec<_> = triggers |
| 94 | .into_iter() |
| 95 | .filter(|t| t.timing == TriggerTiming::Before) |
| 96 | .collect(); |
| 97 | |
| 98 | if before_triggers.is_empty() { |
| 99 | return Ok(new_fields.clone()); |
| 100 | } |
| 101 | |
| 102 | check_cascade_depth(cascade_depth, collection)?; |
| 103 | |
| 104 | let bindings = RowBindings::before_update(collection, old_fields.clone(), new_fields.clone()); |
| 105 | |
| 106 | let result = fire_before_triggers_with_mutation( |
| 107 | state, |
| 108 | identity, |
| 109 | tenant_id, |
| 110 | collection, |
| 111 | &before_triggers, |
| 112 | &bindings, |
| 113 | cascade_depth, |
| 114 | Some(new_fields.clone()), |
| 115 | ) |
| 116 | .await?; |
| 117 | |
| 118 | Ok(result.unwrap_or_else(|| new_fields.clone())) |
| 119 | } |
| 120 | |
| 121 | /// Fire BEFORE ROW triggers for a DELETE operation. |
| 122 | /// |
no test coverage detected