rocket_contrib::databases

Function database_config

Source
pub fn database_config<'a>(
    name: &str,
    from: &'a Config,
) -> Result<DatabaseConfig<'a>, ConfigError>
Expand description

Retrieves the database configuration for the database named name.

This function is primarily used by the code generated by the #[database] attribute.

ยงExample

Consider the following configuration:

[global.databases]
my_db = { url = "db/db.sqlite", pool_size = 25 }
my_other_db = { url = "mysql://root:root@localhost/database" }

The following example uses database_config to retrieve the configurations for the my_db and my_other_db databases:

use rocket_contrib::databases::{database_config, ConfigError};

let config = database_config("my_db", rocket.config()).unwrap();
assert_eq!(config.url, "db/db.sqlite");
assert_eq!(config.pool_size, 25);

let other_config = database_config("my_other_db", rocket.config()).unwrap();
assert_eq!(other_config.url, "mysql://root:root@localhost/database");

let error = database_config("invalid_db", rocket.config()).unwrap_err();
assert_eq!(error, ConfigError::MissingKey);