logo
pub struct ExampleDb(_);
Expand description

Example of code generated by the #[database] attribute.

This implementation of ExampleDb was generated by:

use rocket_sync_db_pools::{database, diesel};

#[database("example")]
pub struct ExampleDb(diesel::SqliteConnection);

Implementations

Returns a fairing that initializes the database connection pool associated with Self.

The fairing must be attached before Self can be used as a request guard.

Example
use rocket_sync_db_pools::diesel;

#[database("my_db")]
struct MyConn(diesel::SqliteConnection);

#[launch]
fn rocket() -> _ {
    rocket::build().attach(MyConn::fairing())
}

Returns an opaque type that represents the connection pool backing connections of type Self as long as the fairing returned by Self::fairing() is attached and has run on __rocket.

The returned pool is Clone. Values of type Self can be retrieved from the pool by calling pool.get().await which has the same signature and semantics as Self::get_one().

Example
use rocket::tokio::{task, time};
use rocket::fairing::AdHoc;
use rocket_sync_db_pools::diesel;

#[database("my_db")]
struct MyConn(diesel::SqliteConnection);

#[launch]
fn rocket() -> _ {
    rocket::build()
        .attach(MyConn::fairing())
        .attach(AdHoc::try_on_ignite("Background DB", |rocket| async {
            let pool = match MyConn::pool(&rocket) {
                Some(pool) => pool.clone(),
                None => return Err(rocket)
            };

            // Start a background task that runs some database
            // operation every 10 seconds. If a connection isn't
            // available, retries 10 + timeout seconds later.
            tokio::task::spawn(async move {
                loop {
                    time::sleep(time::Duration::from_secs(10)).await;
                    if let Some(conn) = pool.get().await {
                        conn.run(|c| { /* perform db ops */ }).await;
                    }
                }
            });

            Ok(rocket)
        }))
}

Runs the provided function __f in an async-safe blocking thread. The function is supplied with a mutable reference to the raw connection (a value of type &mut Self.0). .awaiting the return value of this function yields the value returned by __f.

Example
use rocket_sync_db_pools::diesel;

#[database("my_db")]
struct MyConn(diesel::SqliteConnection);

#[get("/")]
async fn f(conn: MyConn) {
    // The type annotation is illustrative and isn't required.
    let result = conn.run(|c: &mut diesel::SqliteConnection| {
        // Use `c`.
    }).await;
}

Retrieves a connection of type Self from the rocket instance. Returns Some as long as Self::fairing() has been attached and there is a connection available within at most timeout seconds.

Trait Implementations

Retrieves a connection from the database pool or fails with a Status::ServiceUnavailable if doing so times out.

The associated error to be returned if derivation fails.

Derives an instance of Self from the incoming request metadata. Read more

Returns true if launch should be aborted and false otherwise.

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

Returns the argument unchanged.

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

Calls U::from(self).

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

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 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.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more