es_query

Macro es_query 

Source
macro_rules! es_query {
    (
        entity = $entity:ident,
        $query:expr,
        $($args:tt)*
    ) => { ... };
    (
        entity = $entity:ident,
        $query:expr
    ) => { ... };
    (
        tbl_prefix = $tbl_prefix:literal,
        $query:expr,
        $($args:tt)*
    ) => { ... };
    (
        tbl_prefix = $tbl_prefix:literal,
        $query:expr
    ) => { ... };
    (
        $query:expr,
        $($args:tt)*
    ) => { ... };
    (
        $query:expr
    ) => { ... };
}
Expand description

Execute an event-sourced query with automatic entity hydration.

Executes user-defined queries and returns entities by internally joining with events table to hydrate entities, essentially giving the illusion of working with just the index table.

Important: This macro only works inside functions (fn) that are defined within structs that have #[derive(EsRepo)] applied. The macro relies on the repository context to properly hydrate entities.

§Returns

Returns an EsQuery struct that provides methods like fetch_one(), fetch_optional(), and fetch_n() for executing the query and retrieving hydrated entities.

§Parameters

  • tbl_prefix: Table prefix to ignore when deriving entity names from table names (optional)
  • entity: Override the entity type (optional, useful when table name doesn’t match entity name)
  • SQL query string
  • Additional arguments for the SQL query (optional)

§Examples

// Basic usage
es_query!("SELECT id FROM users WHERE id = $1", id)

// With table prefix
es_query!(
    tbl_prefix = "app",
    "SELECT id FROM app_users WHERE active = true"
)

// With custom entity type
es_query!(
    entity = User,
    "SELECT id FROM custom_users_table WHERE id = $1",
    id as UserId
)