Module diesel

Source
Expand description

Re-export of diesel with prelude types overridden with async variants from diesel_async.

§Usage

To use async diesel support provided here, enable the following dependencies in your Cargo.toml:

[dependencies]
rocket = "0.5.1"
diesel = "2"

[dependencies.rocket_db_pools]
version = "0.2.0"
features = ["diesel_mysql"]

Then, import rocket_db_pools::diesel::prelude::* as well as the appropriate pool type and, optionally, QueryResult. To use macros or diesel functions, use diesel:: directly. That is, do not import rocket_db_pools::diesel. Doing so will, by design, cause import errors.

§Example

use rocket_db_pools::{Database, Connection};
use rocket_db_pools::diesel::{QueryResult, MysqlPool, prelude::*};

#[derive(Database)]
#[database("diesel_mysql")]
struct Db(MysqlPool);

#[derive(Queryable, Insertable)]
#[diesel(table_name = posts)]
struct Post {
    id: i64,
    title: String,
    published: bool,
}

diesel::table! {
    posts (id) {
        id -> BigInt,
        title -> Text,
        published -> Bool,
    }
}

#[get("/")]
async fn list(mut db: Connection<Db>) -> QueryResult<String> {
    let post_ids: Vec<i64> = posts::table
        .select(posts::id)
        .load(&mut db)
        .await?;

    Ok(format!("{post_ids:?}"))
}

Modules§

prelude
The diesel prelude with sync-only traits replaced with their diesel_async variants.

Structs§

AsyncMysqlConnection
A connection to a MySQL database. Connection URLs should be in the form mysql://[user[:password]@]host/database_name
AsyncPgConnection
A connection to a PostgreSQL database.

Type Aliases§

MysqlPool
Type alias for an async pool of MySQL connections for async diesel.
PgPool
Type alias for an async pool of Postgres connections for async diesel.
Pool
Type alias for using deadpool::managed::Pool with [diesel-async]
QueryResult
Alias of a Result with an error type of Debug for a diesel::Error.