Skip to main content

better_auth_diesel_sqlite/
config.rs

1//! Adapter configuration types.
2
3use std::time::Duration;
4
5/// Connection pool configuration for the `SQLite` adapter.
6///
7/// Controls pool sizing, timeouts, and connection lifecycle.
8#[derive(Debug, Clone)]
9pub struct PoolConfig {
10    /// Maximum number of connections in the pool.
11    pub max_connections: u32,
12    /// Minimum number of idle connections to maintain.
13    pub min_connections: u32,
14    /// Maximum time to wait for a connection from the pool.
15    pub acquire_timeout: Duration,
16    /// Maximum idle time before a connection is closed.
17    pub idle_timeout: Duration,
18    /// Maximum lifetime of a connection.
19    pub max_lifetime: Duration,
20    /// Whether to run embedded migrations on adapter construction.
21    pub run_migrations: bool,
22}
23
24impl Default for PoolConfig {
25    fn default() -> Self {
26        Self {
27            max_connections: 8,
28            min_connections: 1,
29            acquire_timeout: Duration::from_secs(30),
30            idle_timeout: Duration::from_secs(600),
31            max_lifetime: Duration::from_secs(1800),
32            run_migrations: true,
33        }
34    }
35}
36
37impl PoolConfig {
38    /// Set the maximum number of connections.
39    #[must_use]
40    pub fn max_connections(mut self, n: u32) -> Self {
41        self.max_connections = n;
42        self
43    }
44
45    /// Set the minimum number of idle connections.
46    #[must_use]
47    pub fn min_connections(mut self, n: u32) -> Self {
48        self.min_connections = n;
49        self
50    }
51
52    /// Set the connection acquisition timeout.
53    #[must_use]
54    pub fn acquire_timeout(mut self, duration: Duration) -> Self {
55        self.acquire_timeout = duration;
56        self
57    }
58
59    /// Set the idle connection timeout.
60    #[must_use]
61    pub fn idle_timeout(mut self, duration: Duration) -> Self {
62        self.idle_timeout = duration;
63        self
64    }
65
66    /// Set the maximum connection lifetime.
67    #[must_use]
68    pub fn max_lifetime(mut self, duration: Duration) -> Self {
69        self.max_lifetime = duration;
70        self
71    }
72
73    /// Set whether to run migrations on construction.
74    #[must_use]
75    pub fn run_migrations(mut self, enabled: bool) -> Self {
76        self.run_migrations = enabled;
77        self
78    }
79}