5 releases

0.1.0 Nov 17, 2023
0.1.0-rc.4 Nov 2, 2023
0.1.0-rc.3 Mar 24, 2023
0.1.0-rc.2 May 9, 2022
0.1.0-rc.1 Jun 9, 2021

#1653 in Procedural macros

Download history 1389/week @ 2023-12-13 1066/week @ 2023-12-20 876/week @ 2023-12-27 1401/week @ 2024-01-03 1655/week @ 2024-01-10 7047/week @ 2024-01-17 2425/week @ 2024-01-24 1404/week @ 2024-01-31 1558/week @ 2024-02-07 1622/week @ 2024-02-14 1161/week @ 2024-02-21 1537/week @ 2024-02-28 1389/week @ 2024-03-06 1638/week @ 2024-03-13 1677/week @ 2024-03-20 1118/week @ 2024-03-27

6,158 downloads per month
Used in 10 crates (via rocket_sync_db_pools)

MIT/Apache

11KB
107 lines

sync_db_pools ci.svg crates.io docs.svg

This crate provides traits, utilities, and a procedural macro for configuring and accessing database connection pools in Rocket. This implementation is backed by r2d2 and exposes connections through request guards.

Usage

First, enable the feature corresponding to your database type:

[dependencies.rocket_sync_db_pools]
version = "0.1.0"
features = ["diesel_sqlite_pool"]

A full list of supported databases and their associated feature names is available in the crate docs. In whichever configuration source you choose, configure a databases dictionary with a key for each database, here sqlite_logs in a TOML source:

[default.databases]
sqlite_logs = { url = "/path/to/database.sqlite" }

In your application's source code:

#[macro_use] extern crate rocket;

use rocket_sync_db_pools::{database, diesel};

#[database("sqlite_logs")]
struct LogsDbConn(diesel::SqliteConnection);

#[get("/logs/<id>")]
async fn get_logs(conn: LogsDbConn, id: usize) -> Result<Logs> {
    conn.run(|c| Logs::by_id(c, id)).await
}

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

See the crate docs for full details.

Dependencies

~0.5–1MB
~23K SLoC