velesdb_core/observer.rs
1//! `DatabaseObserver` — extension hook for velesdb-premium.
2//!
3//! The core library has zero knowledge of Premium internals.
4//! Premium implements this trait and injects it via
5//! [`Database::open_with_observer`](crate::Database::open_with_observer).
6//!
7//! # Contract
8//!
9//! - All methods have default no-op implementations.
10//! - Implementations MUST be `Send + Sync`.
11//! - Implementations MUST NOT panic.
12//! - Overhead when `observer` is `None` is a single pointer check.
13
14use crate::collection::CollectionType;
15
16/// Lifecycle hooks for database events.
17///
18/// Implement this trait in `velesdb-premium` to attach RBAC, audit logging,
19/// multi-tenant routing, or replication logic without modifying the core.
20///
21/// # Example (Premium side)
22///
23/// ```rust,ignore
24/// use velesdb_core::{DatabaseObserver, CollectionType};
25///
26/// struct PremiumObserver { /* audit_log, rbac, tenant_router */ }
27///
28/// impl DatabaseObserver for PremiumObserver {
29/// fn on_collection_created(&self, name: &str, kind: &CollectionType) {
30/// // self.audit_log.record(...)
31/// }
32/// }
33/// ```
34pub trait DatabaseObserver: Send + Sync {
35 /// Called after a collection is successfully created.
36 fn on_collection_created(&self, _name: &str, _kind: &CollectionType) {}
37
38 /// Called after a collection is successfully deleted.
39 fn on_collection_deleted(&self, _name: &str) {}
40
41 /// Called after points are upserted into a collection.
42 fn on_upsert(&self, _collection: &str, _point_count: usize) {}
43
44 /// Called after a query is executed, with the duration in microseconds.
45 fn on_query(&self, _collection: &str, _duration_us: u64) {}
46
47 /// Called before a DDL statement is executed.
48 ///
49 /// Premium extensions can implement this to enforce RBAC policies
50 /// (e.g., only admin users can CREATE/DROP collections).
51 ///
52 /// Returns `Ok(())` to allow the DDL operation, or `Err(Error)` to reject it.
53 /// Default implementation allows all DDL operations.
54 ///
55 /// # Errors
56 ///
57 /// Implementations should return an error to reject the DDL operation.
58 fn on_ddl_request(&self, operation: &str, collection_name: &str) -> crate::Result<()> {
59 let _ = (operation, collection_name);
60 Ok(())
61 }
62
63 /// Called before a mutating DML statement is executed.
64 ///
65 /// Premium extensions can implement this to enforce RBAC policies
66 /// (e.g., restrict INSERT EDGE, DELETE, or DELETE EDGE to authorized users).
67 ///
68 /// Returns `Ok(())` to allow the DML mutation, or `Err(Error)` to reject it.
69 /// Default implementation allows all DML mutations.
70 ///
71 /// # Errors
72 ///
73 /// Implementations should return an error to reject the DML mutation.
74 fn on_dml_mutation_request(&self, operation: &str, collection_name: &str) -> crate::Result<()> {
75 let _ = (operation, collection_name);
76 Ok(())
77 }
78}