#[query]Expand description
Marks a function as a query (read-only, cacheable, subscribable).
§Attributes
cache = "5m"- Cache TTL (duration like “30s”, “5m”, “1h”)public- No authentication requiredrequire_auth- Require authenticationtimeout = 30- Timeout in secondstables = ["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]
pub async fn get_user(ctx: &QueryContext, user_id: Uuid) -> Result<User> {
// Tables automatically extracted from SQL
}
#[forge::query(cache = "5m", require_auth)]
pub async fn get_profile(ctx: &QueryContext) -> Result<Profile> {
let user_id = ctx.require_user_id()?;
// ...
}
#[forge::query(tables = ["users", "audit_log"])]
pub async fn dynamic_query(ctx: &QueryContext, table: String) -> Result<Vec<Row>> {
// Explicit tables for dynamic SQL
}