pub struct AggregatorBuilder { /* private fields */ }Expand description
Builder for creating and committing events.
Use create() or aggregator() to create an instance, then chain
method calls to add events and metadata before committing.
§Optimistic Concurrency
If original_version is 0 (default for new aggregates), the builder
queries the current version before writing. Otherwise, it uses the
provided version for optimistic concurrency control.
§Example
// New aggregate
let id = create()
.event(&MyEvent { ... })
.commit(&executor)
.await?;
// Existing aggregate with version check
aggregator(&id)
.original_version(5)
.event(&AnotherEvent { ... })
.commit(&executor)
.await?;Implementations§
Source§impl AggregatorBuilder
impl AggregatorBuilder
Sourcepub fn new(aggregator_id: impl Into<String>) -> AggregatorBuilder
pub fn new(aggregator_id: impl Into<String>) -> AggregatorBuilder
Creates a new builder for the given aggregate ID.
Sourcepub fn ids(ids: Vec<impl Into<String>>) -> AggregatorBuilder
pub fn ids(ids: Vec<impl Into<String>>) -> AggregatorBuilder
Creates a new builder for the given aggregate IDs.
Sourcepub fn original_version(&mut self, v: u16) -> &mut Self
pub fn original_version(&mut self, v: u16) -> &mut Self
Sets the expected version for optimistic concurrency control.
If the aggregate’s current version doesn’t match, the commit will fail
with WriteError::InvalidOriginalVersion.
Sourcepub fn routing_key(&mut self, v: impl Into<String>) -> &mut Self
pub fn routing_key(&mut self, v: impl Into<String>) -> &mut Self
Sets the routing key for event distribution.
The routing key is used for partitioning events across consumers. Once set, subsequent calls are ignored (locked behavior).
Sourcepub fn routing_key_opt(&mut self, v: Option<String>) -> &mut Self
pub fn routing_key_opt(&mut self, v: Option<String>) -> &mut Self
Sets an optional routing key for event distribution.
Pass None to explicitly clear the routing key.
Once set, subsequent calls are ignored (locked behavior).
Sourcepub fn metadata<M>(&mut self, key: impl Into<String>, value: &M) -> &mut Selfwhere
M: Encode,
pub fn metadata<M>(&mut self, key: impl Into<String>, value: &M) -> &mut Selfwhere
M: Encode,
Sets the metadata to attach to all events.
Metadata is serialized using bitcode and stored alongside each event.
pub fn requested_by(&mut self, value: impl Into<String>) -> &mut Self
pub fn requested_as(&mut self, value: impl Into<String>) -> &mut Self
Sourcepub fn metadata_from(&mut self, value: impl Into<Metadata>) -> &mut Self
pub fn metadata_from(&mut self, value: impl Into<Metadata>) -> &mut Self
Sets the metadata to attach to all events.
Metadata is serialized using bitcode and stored alongside each event.
Sourcepub fn event<D>(&mut self, v: &D) -> &mut Selfwhere
D: AggregatorEvent + Encode,
pub fn event<D>(&mut self, v: &D) -> &mut Selfwhere
D: AggregatorEvent + Encode,
Adds an event to be committed.
Multiple events can be added and will be committed atomically. The event data is serialized using bitcode.
Sourcepub async fn commit<E: Executor>(
&self,
executor: &E,
) -> Result<String, WriteError>
pub async fn commit<E: Executor>( &self, executor: &E, ) -> Result<String, WriteError>
Commits all added events to the executor.
Returns the aggregate ID on success.
§Errors
WriteError::MissingData- No events were addedWriteError::InvalidOriginalVersion- Version conflict occurredWriteError::Unknown- Executor error
Trait Implementations§
Source§impl Clone for AggregatorBuilder
impl Clone for AggregatorBuilder
Source§fn clone(&self) -> AggregatorBuilder
fn clone(&self) -> AggregatorBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more