pub struct TimestampBehavior {
pub created_field: &'static str,
pub updated_field: &'static str,
}Expand description
自动填充时间戳 Behavior
§示例
use sz_orm_model::behaviors::{Behavior, TimestampBehavior};
use sz_orm_model::hooks::HookContext;
use sz_orm_model::Value;
use std::collections::HashMap;
let b = TimestampBehavior::new("created_at", "updated_at");
let ctx = HookContext::default().with_timestamp(1700000000);
let mut attrs = HashMap::new();
b.before_insert(&ctx, &mut attrs).unwrap();
assert_eq!(attrs.get("created_at"), Some(&Value::I64(1700000000)));
assert_eq!(attrs.get("updated_at"), Some(&Value::I64(1700000000)));Fields§
§created_field: &'static str创建时间字段名(默认 “created_at”)
updated_field: &'static str更新时间字段名(默认 “updated_at”)
Implementations§
Source§impl TimestampBehavior
impl TimestampBehavior
Sourcepub fn new(
created_field: &'static str,
updated_field: &'static str,
) -> TimestampBehavior
pub fn new( created_field: &'static str, updated_field: &'static str, ) -> TimestampBehavior
创建默认配置的 TimestampBehavior(字段名 created_at/updated_at)
Sourcepub fn default_fields() -> TimestampBehavior
pub fn default_fields() -> TimestampBehavior
使用默认字段名(created_at/updated_at)
Trait Implementations§
Source§impl Behavior for TimestampBehavior
impl Behavior for TimestampBehavior
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 TimestampBehavior
impl RefUnwindSafe for TimestampBehavior
impl Send for TimestampBehavior
impl Sync for TimestampBehavior
impl Unpin for TimestampBehavior
impl UnsafeUnpin for TimestampBehavior
impl UnwindSafe for TimestampBehavior
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