I am building an ‘iced+sqlx’-app, which needs connections to a postgres db. Normally I would create a connection in main and pass it along as an argument to helper sql functions.
#[tokio::main]
async fn main() -> Result<(), IcedError> {
// we need to activate the db pool
let pool = PgPool::connect("postgres://alex:1234@localhost/kedoiced")
.await
.unwrap();
//
//
iced::application("Experiment", Keto::update, Keto::view)
.theme(Keto::theme)
.run_with(Keto::new)
}
But I see no way to pass it along to the iced application?
The only solution I see now is to create a separate connection to the database in each async helper function, which handles sql. And just call it from Task::perform(… ).
pub async fn save(&self) -> Result<uuid::Uuid, anyhow::Error> {
let pool = PgPool::connect("postgres://alex:1234@localhost/ketoiced")
.await
.unwrap();
let rec = sqlx::query!(
r#"
INSERT INTO "macro_food"(name, protein, carbohydrates, fat, weight, kcalories)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING macro_id
"#,
self.name,
self.protein,
self.carbohydrates,
self.fat,
self.weight,
self.kcalories
)
.fetch_one(&pool)
.await?;
Ok(rec.macro_id)
}
Is this the only way?
I would appreciate any help in this regard. I am still learning Rust/Iced/… but I can’t find any documentation specific to this.