DbConfig

Struct DbConfig 

Source
pub struct DbConfig {
    pub database_url: String,
    pub min_connections: u32,
    pub max_connections: u32,
    pub acquire_timeout: Duration,
    pub idle_timeout: Duration,
    pub test_before_acquire: bool,
}
Expand description

Settings used to establish a connection with a database, regardless of the engine. All the values can be initialized with DbConfig::init_for() method, that uses environment variables to setup all of them, otherwise all have default values, except the string connection.

Fields§

§database_url: String

Database URL, initialized with the DATABASE_URL env

§min_connections: u32

Min connections created at start-up, value set with MIN_CONNECTIONS env, default 1

§max_connections: u32

Max connections allowed, value set with MAX_CONNECTIONS env, default 10

§acquire_timeout: Duration

Time allowed to acquire a connection, value set with ACQUIRE_TIMEOUT_MS env, default 750 milliseconds

§idle_timeout: Duration

Max time a connection can be idle, value set with IDLE_TIMEOUT_SEC env, default 300 sec (5 min). Any connection that remains in the idle queue longer than this will be closed.

§test_before_acquire: bool

Whether to test before test the connection at start-up or not, value set with TEST_BEFORE_ACQUIRE env, default to false

Implementations§

Source§

impl DbConfig

Source

pub fn init_for(env: &Environment) -> Result<Self>

Init the object with env passed, and the rest of the attributes reading its corresponding environment variable, otherwise use a default value.

The database string is saved in self.database_url with the value found at the DATABASE_URL environment value, that it’s the only one required (there is no default value). If env passed is Environment::Test the prefix _test is added to the string connection, to avoid using by mistake prod/local databases, unless the string already ends with the prefix, or the string has connection arguments (the ? symbol in the string).

§Examples
use std::env;
use server_env_config::db::DbConfig;
use server_env_config::env::Environment;

// Configurations should be actually set by the OS environment
env::set_var("DATABASE_URL", "postgresql://user:pass@localhost/db");
env::set_var("MAX_CONNECTIONS", "50");
env::set_var("IDLE_TIMEOUT_SEC", "60");

let db = DbConfig::init_for(&Environment::Local).unwrap();

assert_eq!(db.database_url, "postgresql://user:pass@localhost/db");
assert_eq!(db.max_connections, 50);
// All settings except DATABASE_URL have default values if env variables are not set
assert_eq!(db.min_connections, 1);
assert!(!db.test_before_acquire);

env::remove_var("DATABASE_URL"); // if not set, DbConfig cannot be initialized
let db = DbConfig::init_for(&Environment::Local);
assert!(db.is_err());

Trait Implementations§

Source§

impl Clone for DbConfig

Source§

fn clone(&self) -> DbConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DbConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ToString for DbConfig

Source§

fn to_string(&self) -> String

This to_string() implementation prints out all the config values in .env format, using as key the environment variable used to set-up the config, even if the configuration was set in another way, e.g. using a default value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.