Skip to main content

velora_core/config/
database.rs

1//! Database configuration types for the Velora platform.
2//!
3//! This module contains configuration structures for different database backends:
4//! - QuestDB (optimized for high-frequency tick data)
5//! - TimescaleDB (PostgreSQL-based, ACID compliant)
6//! - InMemory (for testing and backtesting)
7
8use serde::{Deserialize, Serialize};
9
10/// Database configuration.
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct DatabaseConfig {
13    /// Database backend type
14    /// Env: VELORA_DATABASE_BACKEND (values: inmemory, questdb, timescaledb)
15    pub backend: DatabaseBackend,
16
17    /// QuestDB configuration (used when backend is QuestDB)
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub questdb: Option<QuestDbConfig>,
20
21    /// TimescaleDB configuration (used when backend is TimescaleDB)
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub timescaledb: Option<TimescaleDbConfig>,
24}
25
26impl Default for DatabaseConfig {
27    fn default() -> Self {
28        DatabaseConfig {
29            backend: DatabaseBackend::InMemory,
30            questdb: None,
31            timescaledb: None,
32        }
33    }
34}
35
36/// Database backend type.
37#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
38#[serde(rename_all = "lowercase")]
39pub enum DatabaseBackend {
40    /// In-memory storage (for testing and backtesting)
41    InMemory,
42    /// QuestDB (optimized for high-frequency tick data)
43    QuestDB,
44    /// TimescaleDB (PostgreSQL-based, ACID compliant)
45    TimescaleDB,
46}
47
48/// QuestDB connection configuration.
49///
50/// QuestDB is a high-performance time-series database optimized for
51/// high-frequency trading tick data.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct QuestDbConfig {
54    /// QuestDB host
55    /// Env: VELORA_DATABASE_QUESTDB_HOST
56    pub host: String,
57
58    /// PostgreSQL wire protocol port (default: 8812)
59    /// Env: VELORA_DATABASE_QUESTDB_PG_PORT
60    pub pg_port: u16,
61
62    /// HTTP API port (default: 9000)
63    /// Env: VELORA_DATABASE_QUESTDB_HTTP_PORT
64    pub http_port: u16,
65
66    /// InfluxDB line protocol port (default: 9009)
67    /// Env: VELORA_DATABASE_QUESTDB_ILP_PORT
68    pub ilp_port: u16,
69
70    /// Database name
71    /// Env: VELORA_DATABASE_QUESTDB_DATABASE
72    pub database: String,
73
74    /// Username (optional)
75    /// Env: VELORA_DATABASE_QUESTDB_USERNAME
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub username: Option<String>,
78
79    /// Password (optional)
80    /// Env: VELORA_DATABASE_QUESTDB_PASSWORD
81    #[serde(skip_serializing_if = "Option::is_none")]
82    pub password: Option<String>,
83
84    /// Connection pool size
85    /// Env: VELORA_DATABASE_QUESTDB_POOL_SIZE
86    #[serde(default = "default_pool_size")]
87    pub pool_size: u32,
88}
89
90impl Default for QuestDbConfig {
91    fn default() -> Self {
92        QuestDbConfig {
93            host: "localhost".to_string(),
94            pg_port: 8812,
95            http_port: 9000,
96            ilp_port: 9009,
97            database: "qdb".to_string(),
98            username: Some("admin".to_string()),
99            password: Some("quest".to_string()),
100            pool_size: 10,
101        }
102    }
103}
104
105/// TimescaleDB connection configuration.
106///
107/// TimescaleDB provides PostgreSQL-based time-series capabilities with
108/// ACID guarantees, suitable for production trading systems.
109#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct TimescaleDbConfig {
111    /// PostgreSQL host
112    /// Env: VELORA_DATABASE_TIMESCALEDB_HOST
113    pub host: String,
114
115    /// PostgreSQL port (default: 5432)
116    /// Env: VELORA_DATABASE_TIMESCALEDB_PORT
117    pub port: u16,
118
119    /// Database name
120    /// Env: VELORA_DATABASE_TIMESCALEDB_DATABASE
121    pub database: String,
122
123    /// Username
124    /// Env: VELORA_DATABASE_TIMESCALEDB_USERNAME
125    pub username: String,
126
127    /// Password
128    /// Env: VELORA_DATABASE_TIMESCALEDB_PASSWORD
129    pub password: String,
130
131    /// Connection pool size
132    /// Env: VELORA_DATABASE_TIMESCALEDB_POOL_SIZE
133    #[serde(default = "default_pool_size")]
134    pub pool_size: u32,
135
136    /// Enable SSL/TLS
137    /// Env: VELORA_DATABASE_TIMESCALEDB_SSL
138    #[serde(default)]
139    pub ssl: bool,
140}
141
142impl Default for TimescaleDbConfig {
143    fn default() -> Self {
144        TimescaleDbConfig {
145            host: "localhost".to_string(),
146            port: 5432,
147            database: "velora".to_string(),
148            username: "postgres".to_string(),
149            password: "".to_string(),
150            pool_size: 10,
151            ssl: false,
152        }
153    }
154}
155
156fn default_pool_size() -> u32 {
157    10
158}