Struct rocket_sync_db_pools::Config
source · [−]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
sourceimpl Config
impl Config
sourcepub fn from(db_name: &str, rocket: &Rocket<Build>) -> Result<Config, Error>
pub fn from(db_name: &str, rocket: &Rocket<Build>) -> Result<Config, Error>
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())
}
sourcepub fn figment(db_name: &str, rocket: &Rocket<Build>) -> Figment
pub fn figment(db_name: &str, rocket: &Rocket<Build>) -> Figment
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
sourceimpl<'de> Deserialize<'de> for Config
impl<'de> Deserialize<'de> for Config
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl StructuralPartialEq for Config
Auto Trait Implementations
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> IntoSql for T
impl<T> IntoSql for T
sourcefn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
fn into_sql<T>(self) -> Self::Expression where
Self: AsExpression<T>,
Convert self
to an expression for Diesel’s query builder. Read more
sourcefn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression where
&'a Self: AsExpression<T>,
Convert &self
to an expression for Diesel’s query builder. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more