MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / fire_before_insert

Function fire_before_insert

nodedb/src/control/trigger/fire_before.rs:31–71  ·  view source on GitHub ↗

Fire BEFORE ROW triggers for an INSERT operation. Returns the (possibly modified) NEW fields. The caller MUST use the returned fields for the actual PointPut dispatch, not the original input — a BEFORE trigger may have normalized or enriched the row. If a trigger raises an exception, the error propagates and the INSERT is aborted.

(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    tenant_id: TenantId,
    collection: &str,
    new_fields: &HashMap<String, nodedb_types::Value>,
    cascade_depth: u32,
)

Source from the content-addressed store, hash-verified

29///
30/// If a trigger raises an exception, the error propagates and the INSERT is aborted.
31pub async fn fire_before_insert(
32 state: &SharedState,
33 identity: &AuthenticatedIdentity,
34 tenant_id: TenantId,
35 collection: &str,
36 new_fields: &HashMap<String, nodedb_types::Value>,
37 cascade_depth: u32,
38) -> crate::Result<HashMap<String, nodedb_types::Value>> {
39 let triggers =
40 state
41 .trigger_registry
42 .get_matching(tenant_id.as_u64(), collection, DmlEvent::Insert);
43
44 let before_triggers: Vec<_> = triggers
45 .into_iter()
46 .filter(|t| t.timing == TriggerTiming::Before)
47 .collect();
48
49 if before_triggers.is_empty() {
50 return Ok(new_fields.clone());
51 }
52
53 check_cascade_depth(cascade_depth, collection)?;
54
55 let bindings = RowBindings::before_insert(collection, new_fields.clone());
56
57 let result = fire_before_triggers_with_mutation(
58 state,
59 identity,
60 tenant_id,
61 collection,
62 &before_triggers,
63 &bindings,
64 cascade_depth,
65 Some(new_fields.clone()),
66 )
67 .await?;
68
69 // Return the (possibly mutated) NEW fields. If None somehow, return original.
70 Ok(result.unwrap_or_else(|| new_fields.clone()))
71}
72
73/// Fire BEFORE ROW triggers for an UPDATE operation.
74///

Callers 2

fire_before_triggersFunction · 0.85

Calls 7

check_cascade_depthFunction · 0.85
get_matchingMethod · 0.80
collectMethod · 0.80
as_u64Method · 0.45
is_emptyMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected