Crates.io | butane_rocket_pool |
lib.rs | butane_rocket_pool |
version | 0.5.3 |
source | src |
created_at | 2021-08-02 12:29:43.604558 |
updated_at | 2024-03-31 05:14:44.686738 |
description | Butane database adapter for Rocket framework |
homepage | |
repository | https://github.com/DimmKG/butane_rocket_pool |
max_upload_size | |
id | 430409 |
size | 7,857 |
Butane database adapter for Rocket framework
Rocket.toml
. Parameters url
and backend_name
are required.[default.databases.test]
backend_name = "sqlite" #Butane's backend name
url = "test.db"
#[database("test")]
struct DbConn(butane_rocket_pool::Connection);
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![create])
.attach(DbConn::fairing())
}
#[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)
}))
}
}