pub async fn load_optional<A: Aggregator, E: Executor>(
executor: &E,
id: impl Into<String>,
) -> Result<Option<LoadResult<A>>, ReadError>Expand description
Load an aggregate by replaying its events, returning None if not found
This is a convenience wrapper around load that returns None instead of
ReadError::NotFound when the aggregate doesn’t exist. This is useful when
you want to handle missing aggregates as a normal case rather than an error.
§Parameters
executor: The event store executor (database connection)id: The aggregate ID to load
§Returns
Returns Ok(Some(LoadResult)) if the aggregate exists, Ok(None) if not found,
or an error for other failure cases.
§Errors
ReadError::TooManyEventsif there are too many events to process (>10 batches)ReadError::BincodeDecodeif event deserialization failsReadError::Unknownfor other database or system errors
Note: Unlike load, this function does NOT return ReadError::NotFound.
§Examples
use evento::load_optional;
async fn get_user_if_exists(executor: &evento::Sqlite, user_id: &str) -> anyhow::Result<Option<User>> {
match load_optional::<User, _>(executor, user_id).await? {
Some(result) => {
println!("Found user at version {}", result.event.version);
Ok(Some(result.item))
}
None => {
println!("User not found");
Ok(None)
}
}
}