Rocket/contrib/sync_db_pools
Sergio Benitez 327b1ad064 Allow sync drops for 'sync_db_pools' connections.
Prior to this commit, connections from 'sync_db_pools' assumed that they
were being dropped in an async context. This is overwhelmingly the
common case as connections are typically dropped immediately after
request processing. Nothing requires this, however, so holding a
connection beyond the scope of the async context was possible (i.e. by
storing a connection in managed state). Given the connection's `Drop`
impl calls `spawn_blocking`, this resulted in a panic on drop.

This commit resolves the issue by modifying `Drop` so that it calls
`spawn_blocking` only when it is executing inside an async context. If
not, the connection is dropped normally, without `spawn_blocking`.
2024-08-18 20:03:01 -07:00
..
codegen Use workspace lints. Resolve new nightly warnings. 2024-05-20 13:39:14 -05:00
lib Allow sync drops for 'sync_db_pools' connections. 2024-08-18 20:03:01 -07:00
README.md Update 'SergioBenitez/Rocket' to 'rwf2/Rocket'. 2023-11-21 16:32:25 +01:00

README.md

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.