pub struct TenantBehavior {
pub tenant_field: &'static str,
pub update_policy: TenantUpdatePolicy,
pub skip_when_no_tenant: bool,
}Expand description
自动填充 tenant_id Behavior
§工作机制
before_insert:若ctx.tenant_id为Some(tid),将tid写入attrs[tenant_field]; 若ctx.tenant_id为None,按skip_when_no_tenant配置决定是跳过还是报错。before_update:根据TenantUpdatePolicy处理 attrs 中的 tenant_id:DenyMismatch(默认):若 attrs 中 tenant_id 与 ctx.tenant_id 不一致则返回DbError::TenantErrorStrip:从 attrs 中移除 tenant_id(保证不被更新)Allow:不做任何处理
§示例
use sz_orm_model::behaviors::{TenantBehavior, TenantUpdatePolicy, Behavior};
use sz_orm_model::hooks::HookContext;
use sz_orm_model::Value;
use std::collections::HashMap;
let b = TenantBehavior::default_fields();
let ctx = HookContext::default().with_tenant(42);
let mut attrs = HashMap::new();
b.before_insert(&ctx, &mut attrs).unwrap();
assert_eq!(attrs.get("tenant_id"), Some(&Value::I64(42)));Fields§
§tenant_field: &'static str租户字段名(默认 “tenant_id”)
update_policy: TenantUpdatePolicyupdate 时对 tenant_id 的处理策略
skip_when_no_tenant: boolctx.tenant_id 为 None 时的行为:
- true:跳过填充(不写入 tenant_id,允许跨租户写入)
- false:返回 TenantError
Implementations§
Source§impl TenantBehavior
impl TenantBehavior
Sourcepub fn new(
tenant_field: &'static str,
update_policy: TenantUpdatePolicy,
skip_when_no_tenant: bool,
) -> TenantBehavior
pub fn new( tenant_field: &'static str, update_policy: TenantUpdatePolicy, skip_when_no_tenant: bool, ) -> TenantBehavior
创建 TenantBehavior
Sourcepub fn default_fields() -> TenantBehavior
pub fn default_fields() -> TenantBehavior
使用默认字段名(tenant_id)+ 默认策略(DenyMismatch + skip_when_no_tenant=true)
Sourcepub fn with_update_policy(self, policy: TenantUpdatePolicy) -> TenantBehavior
pub fn with_update_policy(self, policy: TenantUpdatePolicy) -> TenantBehavior
设置 update 策略(builder 风格)
Sourcepub fn with_skip_when_no_tenant(self, skip: bool) -> TenantBehavior
pub fn with_skip_when_no_tenant(self, skip: bool) -> TenantBehavior
设置 ctx.tenant_id 为 None 时的行为(builder 风格)
Trait Implementations§
Source§impl Behavior for TenantBehavior
impl Behavior for TenantBehavior
Source§fn before_insert(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
fn before_insert( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
在 insert 前触发(默认空实现)
Source§fn before_update(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
fn before_update( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
在 update 前触发(默认空实现)
Source§fn before_delete(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
fn before_delete( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
在 delete 前触发(默认空实现)
Source§fn after_find(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> Result<(), DbError>
fn after_find( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> Result<(), DbError>
在 find 后触发(默认空实现,可用于字段后处理)
Auto Trait Implementations§
impl Freeze for TenantBehavior
impl RefUnwindSafe for TenantBehavior
impl Send for TenantBehavior
impl Sync for TenantBehavior
impl Unpin for TenantBehavior
impl UnsafeUnpin for TenantBehavior
impl UnwindSafe for TenantBehavior
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