forked from
slices.network/slices
Highly ambitious ATProtocol AppView service and sdks
1//! Database query parameter types.
2//!
3//! This module contains types used for building dynamic SQL queries,
4//! including WHERE conditions, sorting, and filtering.
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::HashMap;
9
10/// Represents a single condition in a WHERE clause.
11///
12/// Supports three types of operations:
13/// - `eq`: Exact match (field = value)
14/// - `in_values`: Array membership (field IN (...))
15/// - `contains`: Pattern matching (field ILIKE '%value%')
16#[derive(Debug, Clone, Serialize, Deserialize)]
17#[serde(rename_all = "camelCase")]
18pub struct WhereCondition {
19 pub eq: Option<Value>,
20 #[serde(rename = "in")]
21 pub in_values: Option<Vec<Value>>,
22 pub contains: Option<String>,
23}
24
25/// Represents a complete WHERE clause with AND/OR conditions.
26///
27/// The main conditions map is combined with AND logic.
28/// The or_conditions map (if present) is combined with OR logic
29/// and the entire OR group is ANDed with the main conditions.
30///
31/// Example JSON:
32/// ```json
33/// {
34/// "collection": {"eq": "app.bsky.feed.post"},
35/// "author": {"eq": "did:plc:123"},
36/// "$or": {
37/// "lang": {"eq": "en"},
38/// "lang": {"eq": "es"}
39/// }
40/// }
41/// ```
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(rename_all = "camelCase")]
44pub struct WhereClause {
45 #[serde(flatten)]
46 pub conditions: HashMap<String, WhereCondition>,
47
48 #[serde(rename = "$or")]
49 pub or_conditions: Option<HashMap<String, WhereCondition>>,
50}
51
52/// Represents a field to sort by with direction.
53///
54/// Used for multi-field sorting in queries.
55#[derive(Debug, Clone, Serialize, Deserialize)]
56#[serde(rename_all = "camelCase")]
57pub struct SortField {
58 pub field: String,
59 pub direction: String,
60}