pub struct Transaction<'a, S: EventStore, C: ConcurrencyStrategy = Unchecked> { /* private fields */ }Expand description
Transaction for appending events to an aggregate instance.
Events are accumulated in the transaction and persisted atomically when
commit() is called. If the transaction is dropped without calling
commit(), the events are silently discarded (rolled back). This allows
errors during event serialization to be handled gracefully.
The C type parameter determines the concurrency strategy:
Unchecked: No version checking (default)Optimistic: Version checked on commit
Implementations§
Source§impl<'a, S: EventStore, C: ConcurrencyStrategy> Transaction<'a, S, C>
impl<'a, S: EventStore, C: ConcurrencyStrategy> Transaction<'a, S, C>
pub const fn new( store: &'a mut S, aggregate_kind: String, aggregate_id: S::Id, expected_version: Option<S::Position>, ) -> Self
Sourcepub fn append<E>(
&mut self,
event: E,
metadata: S::Metadata,
) -> Result<(), <S::Codec as Codec>::Error>where
E: SerializableEvent,
pub fn append<E>(
&mut self,
event: E,
metadata: S::Metadata,
) -> Result<(), <S::Codec as Codec>::Error>where
E: SerializableEvent,
Append an event to the transaction.
For sum-type events (enums), this serializes each variant to its persistable form.
§Errors
Returns a codec error if serialization fails.
Source§impl<S: EventStore> Transaction<'_, S, Unchecked>
impl<S: EventStore> Transaction<'_, S, Unchecked>
Source§impl<S: EventStore> Transaction<'_, S, Optimistic>
impl<S: EventStore> Transaction<'_, S, Optimistic>
Sourcepub async fn commit(self) -> Result<(), AppendError<S::Position, S::Error>>
pub async fn commit(self) -> Result<(), AppendError<S::Position, S::Error>>
Commit the transaction with version checking.
The commit will fail with a ConcurrencyConflict if the stream
version has changed since the aggregate was loaded.
When expected_version is None, this means we expect a new aggregate
(empty stream). The commit will fail with a conflict if the stream
already has events.
§Errors
Returns AppendError::Conflict if another writer modified the stream,
or AppendError::Store if persistence fails.