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

#2328 in Database interfaces

Download history 1308/week @ 2024-07-22 1308/week @ 2024-07-29 1066/week @ 2024-08-05 1784/week @ 2024-08-12 1332/week @ 2024-08-19 1486/week @ 2024-08-26 1555/week @ 2024-09-02 1510/week @ 2024-09-09 1422/week @ 2024-09-16 1475/week @ 2024-09-23 1203/week @ 2024-09-30 700/week @ 2024-10-07 1067/week @ 2024-10-14 1348/week @ 2024-10-21 1213/week @ 2024-10-28 1336/week @ 2024-11-04

4,997 downloads per month
Used in 11 crates (10 directly)

MIT/Apache

1MB
14K SLoC

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

~14–50MB
~881K SLoC