macro rocket_sync_db_pools_codegen

Procedural macros for rocket_sync_db_pools

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

#1953 in Procedural macros

Download history 1264/week @ 2024-07-21 1327/week @ 2024-07-28 1093/week @ 2024-08-04 1692/week @ 2024-08-11 1432/week @ 2024-08-18 1410/week @ 2024-08-25 1639/week @ 2024-09-01 1495/week @ 2024-09-08 1464/week @ 2024-09-15 1467/week @ 2024-09-22 1259/week @ 2024-09-29 777/week @ 2024-10-06 1034/week @ 2024-10-13 1292/week @ 2024-10-20 1232/week @ 2024-10-27 1395/week @ 2024-11-03

4,998 downloads per month
Used in 12 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.4–0.9MB
~20K SLoC