Middleware

Trait Middleware 

Source
pub trait Middleware: Send + Sync {
    // Required method
    fn handle<'a>(
        &'a self,
        ctx: QueryContext,
        next: Next<'a>,
    ) -> BoxFuture<'a, MiddlewareResult<QueryResponse>>;

    // Provided methods
    fn name(&self) -> &'static str { ... }
    fn enabled(&self) -> bool { ... }
}
Expand description

Middleware trait for intercepting queries.

Implement this trait to create custom middleware that can:

  • Modify queries before execution
  • Modify responses after execution
  • Short-circuit execution (e.g., for caching)
  • Add logging, metrics, or other side effects

§Example

use prax_query::middleware::{Middleware, QueryContext, QueryResponse, Next, MiddlewareResult};

struct MyMiddleware;

impl Middleware for MyMiddleware {
    fn handle<'a>(
        &'a self,
        ctx: QueryContext,
        next: Next<'a>,
    ) -> BoxFuture<'a, MiddlewareResult<QueryResponse>> {
        Box::pin(async move {
            // Before query
            println!("Executing: {}", ctx.sql());

            // Call next middleware or execute query
            let response = next.run(ctx).await?;

            // After query
            println!("Completed in {}us", response.execution_time_us);

            Ok(response)
        })
    }
}

Required Methods§

Source

fn handle<'a>( &'a self, ctx: QueryContext, next: Next<'a>, ) -> BoxFuture<'a, MiddlewareResult<QueryResponse>>

Handle a query, optionally calling the next handler.

Provided Methods§

Source

fn name(&self) -> &'static str

Name of this middleware (for debugging/logging).

Source

fn enabled(&self) -> bool

Whether this middleware is enabled.

Implementors§