Struct rocket_sync_db_pools::Config[][src]

pub struct Config {
    pub url: String,
    pub pool_size: u32,
    pub timeout: u8,
}
Expand description

A base Config for any Poolable type.

For the following configuration:

[global.databases.my_database]
url = "postgres://root:root@localhost/my_database"
pool_size = 10
timeout = 5

Config::from("my_database", rocket) would return the following struct:

Config {
    url: "postgres://root:root@localhost/my_database".into(),
    pool_size: 10,
    timeout: 5
};

If you want to implement your own custom database adapter (or other database-like struct that can be pooled by r2d2) and need some more configurations options, you may need to define a custom Config struct. Note, however, that the configuration values in Config are required.

Fields

url: String

Connection URL specified in the Rocket configuration.

pool_size: u32

Initial pool size. Defaults to the number of Rocket workers * 4.

timeout: u8

How long to wait, in seconds, for a new connection before timing out. Defaults to 5.

Implementations

Retrieves the database configuration for the database named name.

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

Example

// Assume that these are the contents of `Rocket.toml`:
[default.databases]
my_db = { url = "db/db.sqlite", pool_size = 25 }
my_other_db = { url = "mysql://root:root@localhost/database" }

use rocket::{Rocket, Build};
use rocket_sync_db_pools::Config;

fn pool(rocket: &Rocket<Build>) {
    let config = Config::from("my_db", rocket).unwrap();
    assert_eq!(config.url, "db/db.sqlite");
    assert_eq!(config.pool_size, 25);

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

    let workers = rocket.figment().extract_inner::<u32>(rocket::Config::WORKERS);
    assert_eq!(config.pool_size, (workers.unwrap() * 4));

    let config = Config::from("unknown_db", rocket);
    assert!(config.is_err())
}

Returns a Figment focused on the configuration for the database with name db_name.

Example

use rocket::{Rocket, Build};
use rocket_sync_db_pools::Config;

fn pool(rocket: &Rocket<Build>) {
    let my_db_figment = Config::figment("my_db", rocket);
    let mysql_prod_figment = Config::figment("mysql_prod", rocket);
}

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Converts self into a collection.

Convert self to an expression for Diesel’s query builder. Read more

Convert &self to an expression for Diesel’s query builder. Read more

Should always be Self

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.