rocket_db_pools/config.rs
1use rocket::serde::{Deserialize, Serialize};
2
3/// Base configuration for all database drivers.
4///
5/// A dictionary matching this structure is extracted from the active
6/// [`Figment`](crate::figment::Figment), scoped to `databases.name`, where
7/// `name` is the name of the database, by the
8/// [`Initializer`](crate::Initializer) fairing on ignition and used to
9/// configure the relevant database and database pool.
10///
11/// With the default provider, these parameters are typically configured in a
12/// `Rocket.toml` file:
13///
14/// ```toml
15/// [default.databases.db_name]
16/// url = "/path/to/db.sqlite"
17///
18/// # Only `url` is required. These have sane defaults and are optional.
19/// min_connections = 64
20/// max_connections = 1024
21/// connect_timeout = 5
22/// idle_timeout = 120
23///
24/// # This option is only supported by the `sqlx_sqlite` driver.
25/// extensions = ["memvfs", "rot13"]
26/// ```
27///
28/// Alternatively, a custom provider can be used. For example, a custom `Figment`
29/// with a global `databases.name` configuration:
30///
31/// ```rust
32/// # use rocket::launch;
33/// #[launch]
34/// fn rocket() -> _ {
35/// let figment = rocket::Config::figment()
36/// .merge(("databases.name", rocket_db_pools::Config {
37/// url: "db:specific@config&url".into(),
38/// min_connections: None,
39/// max_connections: 1024,
40/// connect_timeout: 3,
41/// idle_timeout: None,
42/// extensions: None,
43/// }));
44///
45/// rocket::custom(figment)
46/// }
47/// ```
48///
49/// For general information on configuration in Rocket, see [`rocket::config`].
50/// For higher-level details on configuring a database, see the [crate-level
51/// docs](crate#configuration).
52// NOTE: Defaults provided by the figment created in the `Initializer` fairing.
53#[derive(Default, Serialize, Deserialize, Debug, Clone, PartialEq)]
54#[serde(crate = "rocket::serde")]
55pub struct Config {
56 /// Database-specific connection and configuration URL.
57 ///
58 /// The format of the URL is database specific; consult your database's
59 /// documentation.
60 pub url: String,
61 /// Minimum number of connections to maintain in the pool.
62 ///
63 /// **Note:** `deadpool` drivers do not support and thus ignore this value.
64 ///
65 /// _Default:_ `None`.
66 pub min_connections: Option<u32>,
67 /// Maximum number of connections to maintain in the pool.
68 ///
69 /// _Default:_ `workers * 4`.
70 pub max_connections: usize,
71 /// Number of seconds to wait for a connection before timing out.
72 ///
73 /// If the timeout elapses before a connection can be made or retrieved from
74 /// a pool, an error is returned.
75 ///
76 /// _Default:_ `5`.
77 pub connect_timeout: u64,
78 /// Maximum number of seconds to keep a connection alive for.
79 ///
80 /// After a connection is established, it is maintained in a pool for
81 /// efficient connection retrieval. When an `idle_timeout` is set, that
82 /// connection will be closed after the timeout elapses. If an
83 /// `idle_timeout` is not specified, the behavior is driver specific but
84 /// typically defaults to keeping a connection active indefinitely.
85 ///
86 /// _Default:_ `None`.
87 pub idle_timeout: Option<u64>,
88 /// A list of database extensions to load at run-time.
89 ///
90 /// **Note:** Only the `sqlx_sqlite` driver supports this option (for SQLite
91 /// extensions) at this time. All other drivers ignore this option.
92 ///
93 /// _Default:_ `None`.
94 pub extensions: Option<Vec<String>>,
95}