disintegrate_macros

Derive Macro StateQuery

Source
#[derive(StateQuery)]
{
    // Attributes available to this derive:
    #[state_query]
    #[id]
}
Expand description

Derives the StateQuery trait for a struct, enabling its use as a state query in Disintegrate.

The state_query attribute is mandatory and must include the event type associated with the state query. Additionally, the id attribute can be utilized to specify the domain identifier of a state query. It is employed in generating a stream query for the state, querying for the event specified in the state_query attribute, with the identifiers marked in or.

It is also possible to rename a state using the rename argument in the state_query attribute. This feature is beneficial for snapshotting, and the name specified in rename is used to identify the snapshot.

§Example


use disintegrate::StateQuery;

#[derive(StateQuery, Clone)]
#[state_query(DomainEvent, rename = "user-query-v1")] // Rename the state for snapshotting
struct UserStateQuery {
    #[id]
    user_id: String,
    name: String
}

In this example, the UserStateQuery struct is annotated with the StateQuery derive, indicating its role as a state query. The #[state_query] attribute specifies the associated event type, and the #[id] attribute is used to define the domain identifiers. The #[state_query] attribute with rename renames the state to ‘user-query-v1’ for snapshotting purposes.