mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
Add a Transaction type to simplify dealing with Transactions
This commit is contained in:
@@ -50,7 +50,9 @@ async fn register(mut req: Request<PgPool>) -> Response {
|
||||
let body: RegisterRequestBody = req.body_json().await.unwrap();
|
||||
let hash = hash_password(&body.password).unwrap();
|
||||
|
||||
let mut pool = req.state();
|
||||
// Make a new transaction
|
||||
let pool = req.state();
|
||||
let mut tx = pool.begin().await.unwrap();
|
||||
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
@@ -62,12 +64,15 @@ RETURNING id, username, email
|
||||
body.email,
|
||||
hash,
|
||||
)
|
||||
.fetch_one(&mut pool)
|
||||
.fetch_one(&mut tx)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let token = generate_token(rec.id).unwrap();
|
||||
|
||||
// Explicitly commit
|
||||
tx.commit().await.unwrap();
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct RegisterResponseBody {
|
||||
user: User,
|
||||
|
||||
Reference in New Issue
Block a user