butane_rocket_pool

Crates.iobutane_rocket_pool
lib.rsbutane_rocket_pool
version0.5.3
sourcesrc
created_at2021-08-02 12:29:43.604558
updated_at2024-03-31 05:14:44.686738
descriptionButane database adapter for Rocket framework
homepage
repositoryhttps://github.com/DimmKG/butane_rocket_pool
max_upload_size
id430409
size7,857
(DimmKG)

documentation

README

butane_rocket_pool

Butane database adapter for Rocket framework

Usage

  1. Configure your database in Rocket.toml. Parameters url and backend_name are required.
[default.databases.test]
backend_name = "sqlite" #Butane's backend name
url = "test.db"
  1. Add and init database in your application's code
#[database("test")]
struct DbConn(butane_rocket_pool::Connection); 

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![create])
    .attach(DbConn::fairing())
}
  1. To use the connection with Butane functions apply two dereference operators.
#[post("/", data = "<post>")]
async fn create(db: DbConn, post: Json<Post>) -> (Status, Value) {
    let result = db.run(move |db| -> Result<Post, butane::Error> {
        let mut result = post.0;
        result.save(&**db)?;
        Post::get(&**db, result.id)
    }).await;

    match result {
        Ok(res) => (Status::Created, json!({
            "message" : "Post is created!",
            "data" : res
        })),
        Err(err) => (Status::InternalServerError, json!({
            "message" : "Can't create post!",
            "error" : format!("{}", err)
        }))
    }
}
Commit count: 18

cargo fmt