Module rocket_db_pools::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§

Structs§

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

Type Aliases§