Skip to main content

aimdb_core/remote/
query.rs

1//! Type-erased persistence query handler for the AimX `record.query` method.
2//!
3//! Kept free of persistence-specific imports so `aimdb-core` need not depend on
4//! `aimdb-persistence`: the handler is a boxed async function registered in the
5//! database's `Extensions` TypeMap by `aimdb_persistence::with_persistence()`,
6//! and invoked by the AimX server dispatch when a client calls `record.query`.
7
8use alloc::boxed::Box;
9use alloc::string::String;
10
11use serde::{Deserialize, Serialize};
12
13/// Type-erased query handler registered by `aimdb-persistence` via Extensions.
14///
15/// A boxed async function that accepts query parameters (record pattern, limit,
16/// start/end timestamps) and returns a JSON value with the results, shaped
17/// `{"records": [QueryRecord…], "total": N}`.
18pub type QueryHandlerFn = Box<
19    dyn Fn(
20            QueryHandlerParams,
21        ) -> core::pin::Pin<
22            Box<dyn core::future::Future<Output = Result<serde_json::Value, String>> + Send>,
23        > + Send
24        + Sync,
25>;
26
27/// One row of a `record.query` result — the canonical shape every transport
28/// shares.
29#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30pub struct QueryRecord {
31    /// Record key / topic the value was stored under (e.g. `"temp.vienna"`).
32    pub topic: String,
33    /// Deserialized record value.
34    pub payload: serde_json::Value,
35    /// Storage timestamp (milliseconds since Unix epoch).
36    pub ts: u64,
37}
38
39/// The pattern a `record.query` with no `name` resolves to: every record, at
40/// every depth.
41pub const QUERY_ALL_PATTERN: &str = "#";
42
43/// Parameters for the type-erased query handler.
44#[derive(Debug, Clone)]
45pub struct QueryHandlerParams {
46    /// Record pattern, MQTT-style: `*` matches one dot-separated segment, `#`
47    /// zero or more (see [`QUERY_ALL_PATTERN`]).
48    pub name: String,
49    /// Maximum results per matching record.
50    pub limit: Option<usize>,
51    /// Optional start timestamp (Unix ms).
52    pub start: Option<u64>,
53    /// Optional end timestamp (Unix ms).
54    pub end: Option<u64>,
55}