Skip to main content

ActiveModel

Struct ActiveModel 

Source
pub struct ActiveModel<M: Model> {
    pub inner: M,
    /* private fields */
}
Expand description

A smart wrapper around a Model tracking modified columns dynamically.

Used to perform targeted, minimal UPDATE or INSERT queries that only persist the columns that have actually changed.

Fields§

§inner: M

The underlying model instance.

Implementations§

Source§

impl<M: Model> ActiveModel<M>

Source

pub fn new_insert(model: M) -> Self

Create a new ActiveModel for an entity that hasn’t been saved yet.

Source

pub fn from_model(model: M) -> Self

Wrap an existing model into an ActiveModel state tracker for updates.

Source

pub fn set<T: ToSql>(&mut self, column: &'static str, value: T)

Flag a column as modified and stage its value for an upcoming transaction.

Source

pub fn has_changes(&self) -> bool

Returns whether any columns have been modified.

Source

pub fn changed_columns(&self) -> Vec<&'static str>

Returns the list of changed column names.

Source

pub fn is_new(&self) -> bool

Evaluates whether the underlying model has not been persisted yet.

Source

pub fn save(&mut self, executor: &mut impl Executor) -> OrmResult<()>

Intelligently issues an INSERT or UPDATE depending on is_new() state.

Validates the model before persisting.

Source

pub fn insert(&mut self, executor: &mut impl Executor) -> OrmResult<()>

Executes a minimal INSERT using only tracked changes.

Validates the model before persisting.

Source

pub fn update(&mut self, executor: &mut impl Executor) -> OrmResult<()>

Executes a focused UPDATE persisting only the changed columns.

Validates the model before persisting. No-op if nothing has changed.

Trait Implementations§

Source§

impl<M: Model> From<M> for ActiveModel<M>

Source§

fn from(model: M) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<M> Freeze for ActiveModel<M>
where M: Freeze,

§

impl<M> RefUnwindSafe for ActiveModel<M>
where M: RefUnwindSafe,

§

impl<M> Send for ActiveModel<M>

§

impl<M> Sync for ActiveModel<M>

§

impl<M> Unpin for ActiveModel<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for ActiveModel<M>
where M: UnsafeUnpin,

§

impl<M> UnwindSafe for ActiveModel<M>
where M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.