Skip to main content

UnitOfWork

Struct UnitOfWork 

Source
pub struct UnitOfWork { /* private fields */ }
Expand description

Tracks and manages all pending changes in a session.

The Unit of Work is responsible for:

  • Maintaining the set of new, dirty, and deleted objects
  • Computing the correct flush order based on FK dependencies
  • Detecting dependency cycles before flush

Implementations§

Source§

impl UnitOfWork

Source

pub fn new() -> Self

Create a new empty Unit of Work.

Source

pub fn register_model<T: Model>(&mut self)

Register a model type for dependency tracking.

This extracts foreign key relationships from the model’s metadata and registers them for flush ordering.

Source

pub fn track_new<T: Model + Serialize>(&mut self, model: &T, key: ObjectKey)

Track a new object for insertion.

The object will be INSERTed during flush.

Source

pub fn track_dirty<T: Model + Serialize>( &mut self, model: &T, key: ObjectKey, changed_columns: Vec<&'static str>, )

Track a dirty object for update.

The object will be UPDATEd during flush (only changed columns).

Source

pub fn track_dirty_auto<T: Model + Serialize>( &mut self, model: &T, key: ObjectKey, )

Track a dirty object for update (auto-detect changed fields).

Uses the change tracker to determine which fields changed.

Source

pub fn track_deleted<T: Model>(&mut self, model: &T, key: ObjectKey)

Track an object for deletion.

The object will be DELETEd during flush.

Source

pub fn snapshot<T: Model + Serialize>(&mut self, key: ObjectKey, model: &T)

Take a snapshot of an object for later dirty detection.

Source

pub fn is_dirty<T: Model + Serialize>(&self, key: &ObjectKey, model: &T) -> bool

Check if an object is dirty (has changed since snapshot).

Source

pub fn changed_fields<T: Model + Serialize>( &self, key: &ObjectKey, model: &T, ) -> Vec<&'static str>

Get the changed fields for an object.

Source

pub fn check_cycles(&self) -> Result<(), UowError>

Check for dependency cycles in the registered tables.

Returns Err(UowError::CycleDetected) if a cycle is found.

Source

pub fn compute_flush_plan(&self) -> Result<FlushPlan, UowError>

Compute the flush plan.

This checks for cycles and orders operations by dependencies.

§Errors

Returns Err if a dependency cycle is detected.

Source

pub fn clear(&mut self)

Clear all tracked changes.

Call this after a successful commit.

Source

pub fn has_changes(&self) -> bool

Check if there are any pending changes.

Source

pub fn pending_count(&self) -> PendingCounts

Get the count of pending operations.

Source

pub fn change_tracker(&self) -> &ChangeTracker

Get a reference to the change tracker.

Source

pub fn change_tracker_mut(&mut self) -> &mut ChangeTracker

Get a mutable reference to the change tracker.

Trait Implementations§

Source§

impl Default for UnitOfWork

Source§

fn default() -> UnitOfWork

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more