spring_sea_orm/
config.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use spring::config::Configurable;
4
5spring::submit_config_schema!("sea-orm", SeaOrmConfig);
6#[cfg(feature = "with-web")]
7spring::submit_config_schema!("sea-orm-web", SeaOrmWebConfig);
8
9#[derive(Debug, Configurable, Clone, JsonSchema, Deserialize)]
10#[config_prefix = "sea-orm"]
11pub struct SeaOrmConfig {
12    /// The URI for connecting to the database. For example:
13    /// * Postgres: `postgres://root:12341234@localhost:5432/myapp_development`
14    /// * Sqlite: `sqlite://db.sqlite?mode=rwc`
15    pub uri: String,
16
17    /// Enable `SQLx` statement logging
18    #[serde(default)]
19    pub enable_logging: bool,
20
21    /// Minimum number of connections for a pool
22    #[serde(default = "default_min_connections")]
23    pub min_connections: u32,
24
25    /// Maximum number of connections for a pool
26    #[serde(default = "default_max_connections")]
27    pub max_connections: u32,
28
29    /// Set the timeout duration when acquiring a connection
30    pub connect_timeout: Option<u64>,
31
32    /// Set a maximum idle duration for individual connections.
33    /// Any connection that remains in the idle queue longer than this will be closed.
34    /// For usage-based database server billing, this can be a cost saver.
35    pub idle_timeout: Option<u64>,
36
37    /// Set the timeout for acquiring a connection
38    pub acquire_timeout: Option<u64>,
39}
40
41fn default_min_connections() -> u32 {
42    1
43}
44
45fn default_max_connections() -> u32 {
46    10
47}
48
49#[cfg(feature = "with-web")]
50#[derive(Debug, Configurable, Clone, JsonSchema, Deserialize)]
51#[config_prefix = "sea-orm-web"]
52pub struct SeaOrmWebConfig {
53    /// Configures whether to expose and assume 1-based page number indexes in the request parameters.
54    /// Defaults to false, meaning a page number of 0 in the request equals the first page.
55    /// If this is set to true, a page number of 1 in the request will be considered the first page.
56    #[serde(default = "default_one_indexed")]
57    pub one_indexed: bool,
58
59    /// Configures the maximum page size to be accepted.
60    /// This allows to put an upper boundary of the page size to prevent potential attacks trying to issue an OOM.
61    /// Defaults to 2000.
62    #[serde(default = "default_max_page_size")]
63    pub max_page_size: u64,
64
65    /// Default page size.
66    #[serde(default = "default_page_size")]
67    pub default_page_size: u64,
68}
69
70#[allow(dead_code)]
71fn default_one_indexed() -> bool {
72    false
73}
74
75#[allow(dead_code)]
76fn default_max_page_size() -> u64 {
77    2000
78}
79
80#[allow(dead_code)]
81fn default_page_size() -> u64 {
82    20
83}