I'm currently developing an app where it is necessary for the frontend to communicate with a PostgreSQL database. I'm using the postgres package to do the PostgreSQL work on the Rust side.
Since I'm using Tauri here, I was building commands to create the connection and delivering data to the user when it's requested.
My problem here is, that I cannot share the PostgreSQL Client between the commands. I know that there is some sort of tauri::State but that cannot handle the client.
Is there a way to share the postgres::Client instance between multiple commands?
My current code looks something like this:
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use postgres::Client;
struct ClientState(Option<Client>);
#[tauri::command]
fn connect(client_state: tauri::State<ClientState>) { ... }
fn main() {
tauri::Builder::default()
.manage(ClientState(None))
.invoke_handler(tauri::generate_handler![connect])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
I want the code to share the instance of the client
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use postgres::Client;
struct ClientState(Option<Client>);
#[tauri::command]
fn connect(client_state: tauri::State<ClientState>) {
// initialize the client
}
#[tauri::command]
fn exists_user(id: &str, client_state: tauri::State<ClientState>) -> bool {
// check if a user with the given id exists for example
}
fn main() {
tauri::Builder::default()
.manage(ClientState(None))
.invoke_handler(tauri::generate_handler![connect, exists_user])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}