Skip to main content

query

Attribute Macro query 

Source
#[query]
Expand description

Marks a function as a query (read-only, cacheable, subscribable).

§Authentication

By default, queries require an authenticated user. Override with:

  • public - No authentication required
  • require_role("admin") - Require specific role

§Attributes

  • cache = "5m" - Cache TTL (duration like “30s”, “5m”, “1h”)
  • log - Enable logging for this query
  • timeout = 30 - Timeout in seconds
  • tables = ["users", "projects"] - Explicit table dependencies (for dynamic SQL)

§Table Dependency Extraction

By default, table dependencies are automatically extracted from SQL strings in the function body at compile time. This enables accurate reactive subscription invalidation for queries that join multiple tables.

For dynamic SQL (e.g., table names built at runtime), use the tables attribute to explicitly specify dependencies.

§Example

#[forge::query]  // Requires authenticated user (default)
pub async fn get_user(ctx: &QueryContext, user_id: Uuid) -> Result<User> {
    // Tables automatically extracted from SQL
}

#[forge::query(public)]  // No auth required
pub async fn get_public_data(ctx: &QueryContext) -> Result<Data> {
    // ...
}

#[forge::query(require_role("admin"), cache = "5m", log)]
pub async fn admin_stats(ctx: &QueryContext) -> Result<Stats> {
    // Requires admin role
}

#[forge::query(tables = ["users", "audit_log"])]
pub async fn dynamic_query(ctx: &QueryContext, table: String) -> Result<Vec<Row>> {
    // Explicit tables for dynamic SQL
}