Skip to main content

fraiseql_core/config/
database.rs

1//! Database connection configuration.
2
3use serde::{Deserialize, Serialize};
4
5/// Database connection configuration.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7#[serde(default)]
8pub struct DatabaseConfig {
9    /// `PostgreSQL` connection URL.
10    pub url: String,
11
12    /// Maximum connections in pool.
13    pub max_connections: u32,
14
15    /// Minimum connections to maintain.
16    pub min_connections: u32,
17
18    /// Connection timeout in seconds.
19    pub connect_timeout_secs: u64,
20
21    /// Query timeout in seconds.
22    pub query_timeout_secs: u64,
23
24    /// Idle timeout in seconds (0 = no timeout).
25    pub idle_timeout_secs: u64,
26
27    /// Enable SSL for database connections.
28    pub ssl_mode: SslMode,
29
30    /// Mutation timing configuration.
31    pub mutation_timing: MutationTimingConfig,
32}
33
34impl Default for DatabaseConfig {
35    fn default() -> Self {
36        Self {
37            url:                  String::new(),
38            max_connections:      10,
39            min_connections:      1,
40            connect_timeout_secs: 10,
41            query_timeout_secs:   30,
42            idle_timeout_secs:    600,
43            ssl_mode:             SslMode::Prefer,
44            mutation_timing:      MutationTimingConfig::default(),
45        }
46    }
47}
48
49/// Mutation timing configuration.
50///
51/// When enabled, the adapter injects `SET LOCAL <variable_name> = clock_timestamp()::text`
52/// before each mutation function call, allowing SQL functions to compute their own
53/// execution duration.
54#[derive(Debug, Clone, Serialize, Deserialize)]
55pub struct MutationTimingConfig {
56    /// Whether mutation timing is enabled.
57    pub enabled: bool,
58
59    /// The PostgreSQL session variable name to set.
60    #[serde(default = "default_timing_variable")]
61    pub variable_name: String,
62}
63
64fn default_timing_variable() -> String {
65    "fraiseql.started_at".to_string()
66}
67
68impl Default for MutationTimingConfig {
69    fn default() -> Self {
70        Self {
71            enabled:       false,
72            variable_name: default_timing_variable(),
73        }
74    }
75}
76
77/// SSL mode for database connections.
78#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
79#[serde(rename_all = "kebab-case")]
80#[non_exhaustive]
81pub enum SslMode {
82    /// Disable SSL.
83    Disable,
84    /// Prefer SSL but allow non-SSL.
85    #[default]
86    Prefer,
87    /// Require SSL.
88    Require,
89    /// Require SSL and verify CA.
90    VerifyCa,
91    /// Require SSL and verify full certificate.
92    VerifyFull,
93}