I have to use two Connection guard. Can I use only one Connection guard #1799
Answered
by
the10thWiz
paulzhang5511
asked this question in
Questions
-
#[derive(Database)]
#[database("demo")]
pub struct Db(sqlx::MySqlPool);
/// whether the user already exists by mobile phone number
pub async fn exist_by_mobile(mut db: Connection<Db>, phone_num: &String) -> Result<bool, sqlx::Error> {
...
}
// save user
pub async fn insert(mut db: Connection<Db>, user: &UserCreate) -> anyhow::Result<u64> {
...
}
#[post("/register", data = "<user>")]
pub async fn register_handle(mut db1: Connection<Db>, mut db2: Connection<Db>, user: Json<UserCreate>) -> Json<WebData<HashMap<String,String>>> {
exist_by_mobile(db1, &user.mobile).await;
...
insert(db2, &user) .await
...
} I have to use two |
Beta Was this translation helpful? Give feedback.
Answered by
the10thWiz
Jul 31, 2021
Replies: 1 comment 4 replies
-
I believe you can use either |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
paulzhang5511
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I believe you can use either
&Connection<Db>
or&mut Connection<Db>
in both exist_by_mobile and insert, which will allow you to reuse the same connection for both.