pub struct BehaviorRegistry { /* private fields */ }Expand description
Behavior 注册中心 — 管理多个 Behavior 的注册与分发
线程安全:内部使用 RwLock,可在多线程环境下共享。
§示例
use sz_orm_model::behaviors::{BehaviorRegistry, TimestampBehavior, BlameableBehavior, Behavior};
use sz_orm_model::hooks::HookContext;
use sz_orm_model::Value;
use std::collections::HashMap;
let mut registry = BehaviorRegistry::new();
registry.register(Box::new(TimestampBehavior::default_fields()));
registry.register(Box::new(BlameableBehavior::default_fields()));
let ctx = HookContext::default().with_operator(100).with_timestamp(1700000000);
let mut attrs = HashMap::new();
registry.before_insert(&ctx, &mut attrs).unwrap();
assert_eq!(attrs.get("created_at"), Some(&Value::I64(1700000000)));
assert_eq!(attrs.get("created_by"), Some(&Value::I64(100)));Implementations§
Source§impl BehaviorRegistry
impl BehaviorRegistry
Sourcepub fn new() -> BehaviorRegistry
pub fn new() -> BehaviorRegistry
创建空的 BehaviorRegistry
Sourcepub fn unregister(&self, name: &str) -> bool
pub fn unregister(&self, name: &str) -> bool
按 name 移除已注册的 Behavior
Sourcepub fn before_insert(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
pub fn before_insert( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
分发 before_insert 事件
Sourcepub fn before_update(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
pub fn before_update( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
分发 before_update 事件
Sourcepub fn before_delete(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
pub fn before_delete( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
分发 before_delete 事件
Sourcepub fn after_find(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
pub fn after_find( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
分发 after_find 事件
Trait Implementations§
Source§impl Default for BehaviorRegistry
impl Default for BehaviorRegistry
Source§fn default() -> BehaviorRegistry
fn default() -> BehaviorRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for BehaviorRegistry
impl !RefUnwindSafe for BehaviorRegistry
impl !UnwindSafe for BehaviorRegistry
impl Send for BehaviorRegistry
impl Sync for BehaviorRegistry
impl Unpin for BehaviorRegistry
impl UnsafeUnpin for BehaviorRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more