1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use ;
use PgPool;
use crateDatabaseConfig;
/// Creates a new PostgreSQL connection pool using the provided `DatabaseConfig`.
///
/// This function establishes a connection pool with the PostgreSQL database
/// based on the parameters in the `DatabaseConfig` struct. It allows you to configure
/// the number of connections, timeouts, and connection lifetime.
///
/// ### Parameters
/// - `config`: A `DatabaseConfig` struct containing the necessary details for connecting
/// to the PostgreSQL database, such as host, port, username, password, etc.
///
/// ### Returns
/// A `Result` containing either a `PgPool` on success or an `anyhow::Error` on failure.
///
/// ### Errors
/// This function returns an error if the connection to the database cannot be established,
/// or if the connection pool options are invalid.
///
/// ### Example
///
/// ```no_run
/// use multitool_hg::database::config::DatabaseConfig;
/// use multitool_hg::database::postgres::new_postgres_pool;
/// use std::time::Duration;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let config = DatabaseConfig {
/// host: "127.0.0.1".to_string(),
/// port: 5432,
/// username: "user".to_string(),
/// password: "password".to_string(),
/// database: "test".to_string(),
/// max_open_cons: 10,
/// min_idle_cons: 5,
/// conn_max_lifetime: Duration::from_secs(900),
/// connection_timeout: Duration::from_secs(15),
/// idle_timeout: Duration::from_secs(3600),
/// };
///
/// let pool = new_postgres_pool(config).await?;
///
/// Ok(())
/// }
/// ```
pub async