mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-11 20:00:29 +01:00
Clean up DB refactor access (#192)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use tauri::{AppHandle, Runtime};
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::UpdateSource;
|
||||
|
||||
const NAMESPACE: &str = "analytics";
|
||||
const NUM_LAUNCHES_KEY: &str = "num_launches";
|
||||
@@ -22,7 +22,6 @@ pub async fn store_launch_history<R: Runtime>(app_handle: &AppHandle<R>) -> Laun
|
||||
info.current_version = app_handle.package_info().version.to_string();
|
||||
|
||||
app_handle
|
||||
.queries()
|
||||
.with_tx(|tx| {
|
||||
info.previous_version =
|
||||
tx.get_key_value_string(NAMESPACE, last_tracked_version_key, "");
|
||||
@@ -39,7 +38,6 @@ pub async fn store_launch_history<R: Runtime>(app_handle: &AppHandle<R>) -> Laun
|
||||
tx.set_key_value_int(NAMESPACE, NUM_LAUNCHES_KEY, info.num_launches, source);
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
info
|
||||
@@ -58,5 +56,5 @@ pub fn get_os() -> &'static str {
|
||||
}
|
||||
|
||||
pub async fn get_num_launches<R: Runtime>(app_handle: &AppHandle<R>) -> i32 {
|
||||
app_handle.queries().connect().await.unwrap().get_key_value_int(NAMESPACE, NUM_LAUNCHES_KEY, 0)
|
||||
app_handle.db().get_key_value_int(NAMESPACE, NUM_LAUNCHES_KEY, 0)
|
||||
}
|
||||
|
||||
@@ -24,12 +24,12 @@ use tokio::fs::{create_dir_all, File};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::sync::watch::Receiver;
|
||||
use tokio::sync::{oneshot, Mutex};
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::models::{
|
||||
Cookie, CookieJar, Environment, HttpRequest, HttpResponse, HttpResponseHeader,
|
||||
HttpResponseState, ProxySetting, ProxySettingAuth,
|
||||
};
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::util::UpdateSource;
|
||||
use yaak_plugins::events::{
|
||||
CallHttpAuthenticationRequest, HttpHeader, RenderPurpose, WindowContext,
|
||||
};
|
||||
@@ -48,16 +48,13 @@ pub async fn send_http_request<R: Runtime>(
|
||||
let plugin_manager = app_handle.state::<PluginManager>();
|
||||
let update_source = &UpdateSource::from_window(&window);
|
||||
let (settings, workspace) = {
|
||||
let db = window.queries().connect().await?;
|
||||
let settings = db.get_or_create_settings(update_source)?;
|
||||
let db = window.db();
|
||||
let settings = db.get_or_create_settings(update_source);
|
||||
let workspace = db.get_workspace(&unrendered_request.workspace_id)?;
|
||||
(settings, workspace)
|
||||
};
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let base_environment =
|
||||
app_handle.db().get_base_environment(&unrendered_request.workspace_id)?;
|
||||
|
||||
let response_id = og_response.id.clone();
|
||||
let response = Arc::new(Mutex::new(og_response.clone()));
|
||||
@@ -84,8 +81,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e.to_string(),
|
||||
&update_source,
|
||||
)
|
||||
.await)
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
@@ -200,8 +196,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
format!("Failed to parse URL \"{}\": {}", url_string, e.to_string()),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
// Yes, we're parsing both URI and URL because they could return different errors
|
||||
@@ -213,8 +208,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
format!("Failed to parse URL \"{}\": {}", url_string, e.to_string()),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -321,8 +315,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e,
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
}
|
||||
} else if body_type == "multipart/form-data" && request_body.contains_key("form") {
|
||||
@@ -353,8 +346,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e.to_string(),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -371,8 +363,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
format!("Invalid mime for multi-part entry {e:?}"),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
} else if !file_path.is_empty() {
|
||||
@@ -388,8 +379,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
format!("Invalid mime for multi-part entry {e:?}"),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -431,8 +421,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e.to_string(),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -463,8 +452,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e.to_string(),
|
||||
&update_source,
|
||||
)
|
||||
.await);
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -490,7 +478,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
Ok(r) = resp_rx => r,
|
||||
_ = cancelled_rx.changed() => {
|
||||
debug!("Request cancelled");
|
||||
return Ok(response_err(&app_handle, &*response.lock().await, "Request was cancelled".to_string(), &update_source).await);
|
||||
return Ok(response_err(&app_handle, &*response.lock().await, "Request was cancelled".to_string(), &update_source));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -541,10 +529,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
|
||||
r.state = HttpResponseState::Connected;
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.update_http_response_if_id(&r, &update_source)
|
||||
.expect("Failed to update response after connected");
|
||||
}
|
||||
@@ -574,10 +559,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
written_bytes += bytes.len();
|
||||
r.content_length = Some(written_bytes as i32);
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.update_http_response_if_id(&r, &update_source)
|
||||
.expect("Failed to update response");
|
||||
}
|
||||
@@ -590,8 +572,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
e.to_string(),
|
||||
&update_source,
|
||||
)
|
||||
.await;
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -606,10 +587,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
};
|
||||
r.state = HttpResponseState::Closed;
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.update_http_response_if_id(&r, &UpdateSource::from_window(&window))
|
||||
.expect("Failed to update response");
|
||||
};
|
||||
@@ -636,10 +614,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
.collect::<Vec<_>>();
|
||||
cookie_jar.cookies = json_cookies;
|
||||
if let Err(e) = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_cookie_jar(&cookie_jar, &UpdateSource::from_window(&window))
|
||||
{
|
||||
error!("Failed to update cookie jar: {}", e);
|
||||
@@ -653,8 +628,7 @@ pub async fn send_http_request<R: Runtime>(
|
||||
&*response.lock().await,
|
||||
format!("{e} → {e:?}"),
|
||||
&update_source,
|
||||
)
|
||||
.await;
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -667,19 +641,14 @@ pub async fn send_http_request<R: Runtime>(
|
||||
Ok(tokio::select! {
|
||||
Ok(r) = done_rx => r,
|
||||
_ = cancelled_rx.changed() => {
|
||||
match app_handle.queries().with_conn(|c| c.get_http_response(&response_id)).await {
|
||||
match app_handle.with_db(|c| c.get_http_response(&response_id)) {
|
||||
Ok(mut r) => {
|
||||
r.state = HttpResponseState::Closed;
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.update_http_response_if_id(&r, &UpdateSource::from_window(window))
|
||||
app_handle.db().update_http_response_if_id(&r, &UpdateSource::from_window(window))
|
||||
.expect("Failed to update response")
|
||||
},
|
||||
_ => {
|
||||
response_err(&app_handle, &*response.lock().await, "Ephemeral request was cancelled".to_string(), &update_source).await
|
||||
response_err(&app_handle, &*response.lock().await, "Ephemeral request was cancelled".to_string(), &update_source)
|
||||
}.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,13 +30,13 @@ use tokio::sync::Mutex;
|
||||
use tokio::task::block_in_place;
|
||||
use yaak_grpc::manager::{DynamicMessage, GrpcHandle};
|
||||
use yaak_grpc::{deserialize_message, serialize_message, Code, ServiceDefinition};
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::models::{
|
||||
CookieJar, Environment, EnvironmentVariable, Folder, GrpcConnection, GrpcConnectionState,
|
||||
GrpcEvent, GrpcEventType, GrpcRequest, HttpRequest, HttpResponse, HttpResponseState, KeyValue,
|
||||
ModelType, Plugin, Settings, WebsocketRequest, Workspace, WorkspaceMeta,
|
||||
};
|
||||
use yaak_models::queries_legacy::{
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::{
|
||||
generate_model_id, get_workspace_export_resources, BatchUpsertResult, UpdateSource,
|
||||
};
|
||||
use yaak_plugins::events::{
|
||||
@@ -108,11 +108,10 @@ async fn cmd_render_template<R: Runtime>(
|
||||
environment_id: Option<&str>,
|
||||
) -> YaakResult<String> {
|
||||
let environment = match environment_id {
|
||||
Some(id) => app_handle.queries().connect().await?.get_environment(id).ok(),
|
||||
Some(id) => app_handle.db().get_environment(id).ok(),
|
||||
None => None,
|
||||
};
|
||||
let base_environment =
|
||||
app_handle.queries().connect().await?.get_base_environment(&workspace_id)?;
|
||||
let base_environment = app_handle.db().get_base_environment(&workspace_id)?;
|
||||
let result = render_template(
|
||||
template,
|
||||
&base_environment,
|
||||
@@ -143,12 +142,7 @@ async fn cmd_grpc_reflect<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
||||
) -> YaakResult<Vec<ServiceDefinition>> {
|
||||
let req = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_grpc_request(request_id)?
|
||||
.ok_or(GenericError("Failed to find GRPC request".to_string()))?;
|
||||
let req = app_handle.db().get_grpc_request(request_id)?;
|
||||
|
||||
let uri = safe_uri(&req.url);
|
||||
|
||||
@@ -175,20 +169,12 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
grpc_handle: State<'_, Mutex<GrpcHandle>>,
|
||||
) -> YaakResult<String> {
|
||||
let environment = match environment_id {
|
||||
Some(id) => app_handle.queries().connect().await?.get_environment(id).ok(),
|
||||
Some(id) => app_handle.db().get_environment(id).ok(),
|
||||
None => None,
|
||||
};
|
||||
let unrendered_request = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_grpc_request(request_id)?
|
||||
.ok_or(GenericError("Failed to get GRPC request".to_string()))?;
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let unrendered_request = app_handle.db().get_grpc_request(request_id)?;
|
||||
let base_environment =
|
||||
app_handle.db().get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let request = render_grpc_request(
|
||||
&unrendered_request,
|
||||
&base_environment,
|
||||
@@ -237,7 +223,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
}
|
||||
}
|
||||
|
||||
let conn = app_handle.queries().connect().await?.upsert_grpc_connection(
|
||||
let conn = app_handle.db().upsert_grpc_connection(
|
||||
&GrpcConnection {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
request_id: request.id.clone(),
|
||||
@@ -289,7 +275,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
let connection = match connection {
|
||||
Ok(c) => c,
|
||||
Err(err) => {
|
||||
app_handle.queries().connect().await?.upsert_grpc_connection(
|
||||
app_handle.db().upsert_grpc_connection(
|
||||
&GrpcConnection {
|
||||
elapsed: start.elapsed().as_millis() as i32,
|
||||
error: Some(err.clone()),
|
||||
@@ -364,10 +350,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
Err(e) => {
|
||||
tauri::async_runtime::spawn(async move {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
event_type: GrpcEventType::Error,
|
||||
@@ -384,10 +367,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
in_msg_tx.try_send(d_msg).unwrap();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: msg,
|
||||
@@ -431,7 +411,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
)
|
||||
.await?;
|
||||
|
||||
app_handle.queries().connect().await?.upsert_grpc_event(
|
||||
app_handle.db().upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: format!("Connecting to {}", req.url),
|
||||
event_type: GrpcEventType::ConnectionStart,
|
||||
@@ -469,10 +449,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
|
||||
if !method_desc.is_client_streaming() {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
event_type: GrpcEventType::ClientMessage,
|
||||
@@ -487,10 +464,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
match maybe_msg {
|
||||
Some(Ok(msg)) => {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
metadata: metadata_to_map(msg.metadata().clone()),
|
||||
@@ -507,10 +481,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
)
|
||||
.unwrap();
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: serialize_message(&msg.into_inner()).unwrap(),
|
||||
@@ -521,10 +492,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
)
|
||||
.unwrap();
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: "Connection complete".to_string(),
|
||||
@@ -538,10 +506,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&(match e.status {
|
||||
Some(s) => GrpcEvent {
|
||||
@@ -572,10 +537,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
let mut stream = match maybe_stream {
|
||||
Some(Ok(stream)) => {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
metadata: metadata_to_map(stream.metadata().clone()),
|
||||
@@ -596,10 +558,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
Some(Err(e)) => {
|
||||
warn!("GRPC stream error {e:?}");
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&(match e.status {
|
||||
Some(s) => GrpcEvent {
|
||||
@@ -631,10 +590,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
Ok(Some(msg)) => {
|
||||
let message = serialize_message(&msg).unwrap();
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: message,
|
||||
@@ -649,10 +605,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
let trailers =
|
||||
stream.trailers().await.unwrap_or_default().unwrap_or_default();
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: "Connection complete".to_string(),
|
||||
@@ -668,10 +621,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
}
|
||||
Err(status) => {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: status.to_string(),
|
||||
@@ -695,12 +645,12 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
let w = app_handle.clone();
|
||||
tokio::select! {
|
||||
_ = grpc_listen => {
|
||||
let events = w.queries().connect().await.unwrap().list_grpc_events(&conn_id).unwrap();
|
||||
let events = w.db().list_grpc_events(&conn_id).unwrap();
|
||||
let closed_event = events
|
||||
.iter()
|
||||
.find(|e| GrpcEventType::ConnectionEnd == e.event_type);
|
||||
let closed_status = closed_event.and_then(|e| e.status).unwrap_or(Code::Unavailable as i32);
|
||||
w.queries().with_conn(|c| {
|
||||
w.with_tx(|c| {
|
||||
c.upsert_grpc_connection(
|
||||
&GrpcConnection{
|
||||
elapsed: start.elapsed().as_millis() as i32,
|
||||
@@ -710,10 +660,10 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
},
|
||||
&UpdateSource::from_window(&window),
|
||||
)
|
||||
}).await.unwrap();
|
||||
}).unwrap();
|
||||
},
|
||||
_ = cancelled_rx.changed() => {
|
||||
w.queries().connect().await.unwrap().upsert_grpc_event(
|
||||
w.db().upsert_grpc_event(
|
||||
&GrpcEvent {
|
||||
content: "Cancelled".to_string(),
|
||||
event_type: GrpcEventType::ConnectionEnd,
|
||||
@@ -722,7 +672,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
},
|
||||
&UpdateSource::from_window(&window),
|
||||
).unwrap();
|
||||
w.queries().with_conn(|c| {
|
||||
w.with_tx(|c| {
|
||||
c.upsert_grpc_connection(
|
||||
&GrpcConnection{
|
||||
elapsed: start.elapsed().as_millis() as i32,
|
||||
@@ -732,7 +682,7 @@ async fn cmd_grpc_go<R: Runtime>(
|
||||
},
|
||||
&UpdateSource::from_window(&window),
|
||||
)
|
||||
}).await.unwrap();
|
||||
}).unwrap();
|
||||
},
|
||||
}
|
||||
w.unlisten(event_handler);
|
||||
@@ -753,11 +703,11 @@ async fn cmd_send_ephemeral_request<R: Runtime>(
|
||||
let response = HttpResponse::default();
|
||||
request.id = "".to_string();
|
||||
let environment = match environment_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_environment(id)?),
|
||||
Some(id) => Some(app_handle.db().get_environment(id)?),
|
||||
None => None,
|
||||
};
|
||||
let cookie_jar = match cookie_jar_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_cookie_jar(id)?),
|
||||
Some(id) => Some(app_handle.db().get_cookie_jar(id)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
@@ -784,7 +734,7 @@ async fn cmd_filter_response<R: Runtime>(
|
||||
plugin_manager: State<'_, PluginManager>,
|
||||
filter: &str,
|
||||
) -> YaakResult<FilterResponse> {
|
||||
let response = app_handle.queries().connect().await?.get_http_response(response_id)?;
|
||||
let response = app_handle.db().get_http_response(response_id)?;
|
||||
|
||||
if let None = response.body_path {
|
||||
return Err(GenericError("Response body path not set".to_string()));
|
||||
@@ -940,20 +890,17 @@ async fn cmd_import_data<R: Runtime>(
|
||||
})
|
||||
.collect();
|
||||
|
||||
let upserted = app_handle
|
||||
.queries()
|
||||
.with_tx(|tx| {
|
||||
tx.batch_upsert(
|
||||
workspaces,
|
||||
environments,
|
||||
folders,
|
||||
http_requests,
|
||||
grpc_requests,
|
||||
websocket_requests,
|
||||
&UpdateSource::Import,
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
let upserted = app_handle.with_tx(|tx| {
|
||||
tx.batch_upsert(
|
||||
workspaces,
|
||||
environments,
|
||||
folders,
|
||||
http_requests,
|
||||
grpc_requests,
|
||||
websocket_requests,
|
||||
&UpdateSource::Import,
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(upserted)
|
||||
}
|
||||
@@ -1077,7 +1024,7 @@ async fn cmd_save_response<R: Runtime>(
|
||||
response_id: &str,
|
||||
filepath: &str,
|
||||
) -> YaakResult<()> {
|
||||
let response = app_handle.queries().connect().await?.get_http_response(response_id)?;
|
||||
let response = app_handle.db().get_http_response(response_id)?;
|
||||
|
||||
let body_path =
|
||||
response.body_path.ok_or(GenericError("Response does not have a body".to_string()))?;
|
||||
@@ -1097,7 +1044,7 @@ async fn cmd_send_http_request<R: Runtime>(
|
||||
// that has not yet been saved in the DB.
|
||||
request: HttpRequest,
|
||||
) -> YaakResult<HttpResponse> {
|
||||
let response = app_handle.queries().connect().await?.upsert_http_response(
|
||||
let response = app_handle.db().upsert_http_response(
|
||||
&HttpResponse {
|
||||
request_id: request.id.clone(),
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
@@ -1114,7 +1061,7 @@ async fn cmd_send_http_request<R: Runtime>(
|
||||
});
|
||||
|
||||
let environment = match environment_id {
|
||||
Some(id) => match app_handle.queries().connect().await?.get_environment(id) {
|
||||
Some(id) => match app_handle.db().get_environment(id) {
|
||||
Ok(env) => Some(env),
|
||||
Err(e) => {
|
||||
warn!("Failed to find environment by id {id} {}", e);
|
||||
@@ -1125,14 +1072,14 @@ async fn cmd_send_http_request<R: Runtime>(
|
||||
};
|
||||
|
||||
let cookie_jar = match cookie_jar_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_cookie_jar(id)?),
|
||||
Some(id) => Some(app_handle.db().get_cookie_jar(id)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
send_http_request(&window, &request, &response, environment, cookie_jar, &mut cancel_rx).await
|
||||
}
|
||||
|
||||
async fn response_err<R: Runtime>(
|
||||
fn response_err<R: Runtime>(
|
||||
app_handle: &AppHandle<R>,
|
||||
response: &HttpResponse,
|
||||
error: String,
|
||||
@@ -1143,10 +1090,7 @@ async fn response_err<R: Runtime>(
|
||||
response.state = HttpResponseState::Closed;
|
||||
response.error = Some(error.clone());
|
||||
response = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.update_http_response_if_id(&response, update_source)
|
||||
.expect("Failed to update response");
|
||||
response
|
||||
@@ -1158,7 +1102,7 @@ async fn cmd_set_update_mode<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<KeyValue> {
|
||||
let (key_value, _created) = app_handle.queries().connect().await?.set_key_value_raw(
|
||||
let (key_value, _created) = app_handle.db().set_key_value_raw(
|
||||
"app",
|
||||
"update_mode",
|
||||
update_mode,
|
||||
@@ -1173,7 +1117,7 @@ async fn cmd_get_key_value<R: Runtime>(
|
||||
key: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Option<KeyValue>> {
|
||||
Ok(app_handle.queries().connect().await?.get_key_value_raw(namespace, key))
|
||||
Ok(app_handle.db().get_key_value_raw(namespace, key))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1184,7 +1128,7 @@ async fn cmd_set_key_value<R: Runtime>(
|
||||
key: &str,
|
||||
value: &str,
|
||||
) -> YaakResult<KeyValue> {
|
||||
let (key_value, _created) = app_handle.queries().connect().await?.set_key_value_raw(
|
||||
let (key_value, _created) = app_handle.db().set_key_value_raw(
|
||||
namespace,
|
||||
key,
|
||||
value,
|
||||
@@ -1205,7 +1149,7 @@ async fn cmd_install_plugin<R: Runtime>(
|
||||
.add_plugin_by_dir(&WindowContext::from_window(&window), &directory, true)
|
||||
.await?;
|
||||
|
||||
Ok(app_handle.queries().connect().await?.upsert_plugin(
|
||||
Ok(app_handle.db().upsert_plugin(
|
||||
&Plugin {
|
||||
directory: directory.into(),
|
||||
url,
|
||||
@@ -1222,11 +1166,8 @@ async fn cmd_uninstall_plugin<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Plugin> {
|
||||
let plugin = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.delete_plugin_by_id(plugin_id, &UpdateSource::from_window(&window))?;
|
||||
let plugin =
|
||||
app_handle.db().delete_plugin_by_id(plugin_id, &UpdateSource::from_window(&window))?;
|
||||
|
||||
plugin_manager
|
||||
.uninstall(&WindowContext::from_window(&window), plugin.directory.as_str())
|
||||
@@ -1241,11 +1182,7 @@ async fn cmd_update_cookie_jar<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<CookieJar> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_cookie_jar(&cookie_jar, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_cookie_jar(&cookie_jar, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1255,9 +1192,7 @@ async fn cmd_delete_cookie_jar<R: Runtime>(
|
||||
cookie_jar_id: &str,
|
||||
) -> YaakResult<CookieJar> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_cookie_jar_by_id(cookie_jar_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1268,7 +1203,7 @@ async fn cmd_create_cookie_jar<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<CookieJar> {
|
||||
Ok(app_handle.queries().connect().await?.upsert_cookie_jar(
|
||||
Ok(app_handle.db().upsert_cookie_jar(
|
||||
&CookieJar {
|
||||
name: name.to_string(),
|
||||
workspace_id: workspace_id.to_string(),
|
||||
@@ -1287,7 +1222,7 @@ async fn cmd_create_environment<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Environment> {
|
||||
Ok(app_handle.queries().connect().await?.upsert_environment(
|
||||
Ok(app_handle.db().upsert_environment(
|
||||
&Environment {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
environment_id: environment_id.map(|s| s.to_string()),
|
||||
@@ -1308,7 +1243,7 @@ async fn cmd_create_grpc_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<GrpcRequest> {
|
||||
Ok(app_handle.queries().connect().await?.upsert_grpc_request(
|
||||
Ok(app_handle.db().upsert_grpc_request(
|
||||
&GrpcRequest {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
name: name.to_string(),
|
||||
@@ -1326,8 +1261,8 @@ async fn cmd_duplicate_grpc_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<GrpcRequest> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let request = db.get_grpc_request(id)?.unwrap();
|
||||
let db = app_handle.db();
|
||||
let request = db.get_grpc_request(id)?;
|
||||
Ok(db.duplicate_grpc_request(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1337,7 +1272,7 @@ async fn cmd_duplicate_folder<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
id: &str,
|
||||
) -> YaakResult<Folder> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let folder = db.get_folder(id)?;
|
||||
Ok(db.duplicate_folder(&folder, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
@@ -1348,8 +1283,8 @@ async fn cmd_duplicate_http_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<HttpRequest> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let request = db.get_http_request(id)?.unwrap();
|
||||
let db = app_handle.db();
|
||||
let request = db.get_http_request(id)?;
|
||||
Ok(db.duplicate_http_request(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1359,11 +1294,7 @@ async fn cmd_update_workspace<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Workspace> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_workspace(&workspace, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_workspace(&workspace, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1373,9 +1304,7 @@ async fn cmd_update_workspace_meta<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<WorkspaceMeta> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.upsert_workspace_meta(&workspace_meta, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1385,11 +1314,7 @@ async fn cmd_update_environment<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Environment> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_environment(&environment, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_environment(&environment, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1398,11 +1323,7 @@ async fn cmd_update_grpc_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<GrpcRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_grpc_request(&request, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_grpc_request(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1411,11 +1332,7 @@ async fn cmd_upsert_http_request<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<HttpRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert(&request, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1425,9 +1342,7 @@ async fn cmd_delete_grpc_request<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<GrpcRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_grpc_request_by_id(request_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1438,9 +1353,7 @@ async fn cmd_delete_http_request<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<HttpRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_http_request_by_id(request_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1449,7 +1362,7 @@ async fn cmd_list_folders<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<Folder>> {
|
||||
Ok(app_handle.queries().connect().await?.list_folders(workspace_id)?)
|
||||
Ok(app_handle.db().list_folders(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1458,11 +1371,7 @@ async fn cmd_update_folder<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Folder> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_folder(&folder, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_folder(&folder, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1471,11 +1380,7 @@ async fn cmd_delete_folder<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
folder_id: &str,
|
||||
) -> YaakResult<Folder> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.delete_folder_by_id(folder_id, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().delete_folder_by_id(folder_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1485,9 +1390,7 @@ async fn cmd_delete_environment<R: Runtime>(
|
||||
environment_id: &str,
|
||||
) -> YaakResult<Environment> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_environment_by_id(environment_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1496,11 +1399,7 @@ async fn cmd_list_grpc_connections<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<GrpcConnection>> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.list_grpc_connections_for_workspace(workspace_id, None)?)
|
||||
Ok(app_handle.db().list_grpc_connections_for_workspace(workspace_id, None)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1508,7 +1407,7 @@ async fn cmd_list_grpc_events<R: Runtime>(
|
||||
connection_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<GrpcEvent>> {
|
||||
Ok(app_handle.queries().connect().await?.list_grpc_events(connection_id)?)
|
||||
Ok(app_handle.db().list_grpc_events(connection_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1516,7 +1415,7 @@ async fn cmd_list_grpc_requests<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<GrpcRequest>> {
|
||||
Ok(app_handle.queries().connect().await?.list_grpc_requests(workspace_id)?)
|
||||
Ok(app_handle.db().list_grpc_requests(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1524,7 +1423,7 @@ async fn cmd_list_http_requests<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<HttpRequest>> {
|
||||
Ok(app_handle.queries().connect().await?.list_http_requests(workspace_id)?)
|
||||
Ok(app_handle.db().list_http_requests(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1533,14 +1432,14 @@ async fn cmd_list_environments<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<Environment>> {
|
||||
// Not sure of a better place to put this...
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
db.ensure_base_environment(workspace_id)?;
|
||||
Ok(db.list_environments(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_list_plugins<R: Runtime>(app_handle: AppHandle<R>) -> YaakResult<Vec<Plugin>> {
|
||||
Ok(app_handle.queries().connect().await?.list_plugins()?)
|
||||
Ok(app_handle.db().list_plugins()?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1562,7 +1461,7 @@ async fn cmd_plugin_info<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
plugin_manager: State<'_, PluginManager>,
|
||||
) -> YaakResult<BootResponse> {
|
||||
let plugin = app_handle.queries().connect().await?.get_plugin(id)?;
|
||||
let plugin = app_handle.db().get_plugin(id)?;
|
||||
Ok(plugin_manager
|
||||
.get_plugin_by_dir(plugin.directory.as_str())
|
||||
.await
|
||||
@@ -1573,11 +1472,7 @@ async fn cmd_plugin_info<R: Runtime>(
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_get_settings<R: Runtime>(window: WebviewWindow<R>) -> YaakResult<Settings> {
|
||||
Ok(window
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_or_create_settings(&UpdateSource::from_window(&window))?)
|
||||
Ok(window.db().get_or_create_settings(&UpdateSource::from_window(&window)))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1586,40 +1481,12 @@ async fn cmd_update_settings<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Settings> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_settings(&settings, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_settings(&settings, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_get_folder<R: Runtime>(id: &str, app_handle: AppHandle<R>) -> YaakResult<Folder> {
|
||||
Ok(app_handle.queries().connect().await?.get_folder(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_get_grpc_request<R: Runtime>(
|
||||
id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Option<GrpcRequest>> {
|
||||
Ok(app_handle.queries().connect().await?.get_grpc_request(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_get_http_request<R: Runtime>(
|
||||
id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Option<HttpRequest>> {
|
||||
Ok(app_handle.queries().connect().await?.get_http_request(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_get_cookie_jar<R: Runtime>(
|
||||
id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<CookieJar> {
|
||||
Ok(app_handle.queries().connect().await?.get_cookie_jar(id)?)
|
||||
Ok(app_handle.db().get_folder(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1628,7 +1495,7 @@ async fn cmd_list_cookie_jars<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Vec<CookieJar>> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let cookie_jars = db.list_cookie_jars(workspace_id)?;
|
||||
|
||||
if cookie_jars.is_empty() {
|
||||
@@ -1648,7 +1515,7 @@ async fn cmd_list_cookie_jars<R: Runtime>(
|
||||
|
||||
#[tauri::command]
|
||||
async fn cmd_list_key_values<R: Runtime>(app_handle: AppHandle<R>) -> YaakResult<Vec<KeyValue>> {
|
||||
Ok(app_handle.queries().connect().await?.list_key_values_raw()?)
|
||||
Ok(app_handle.db().list_key_values_raw()?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1656,7 +1523,7 @@ async fn cmd_get_environment<R: Runtime>(
|
||||
id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Environment> {
|
||||
Ok(app_handle.queries().connect().await?.get_environment(id)?)
|
||||
Ok(app_handle.db().get_environment(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1664,7 +1531,7 @@ async fn cmd_get_workspace<R: Runtime>(
|
||||
id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Workspace> {
|
||||
Ok(app_handle.queries().connect().await?.get_workspace(id)?)
|
||||
Ok(app_handle.db().get_workspace(id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1673,11 +1540,7 @@ async fn cmd_list_http_responses<R: Runtime>(
|
||||
limit: Option<i64>,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> YaakResult<Vec<HttpResponse>> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.list_http_responses_for_workspace(workspace_id, limit.map(|l| l as u64))?)
|
||||
Ok(app_handle.db().list_http_responses_for_workspace(workspace_id, limit.map(|l| l as u64))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1686,7 +1549,7 @@ async fn cmd_delete_http_response<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<HttpResponse> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let http_response = db.get_http_response(id)?;
|
||||
Ok(db.delete_http_response(&http_response, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
@@ -1697,11 +1560,7 @@ async fn cmd_delete_grpc_connection<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<GrpcConnection> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.delete_grpc_connection_by_id(id, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().delete_grpc_connection_by_id(id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1711,9 +1570,7 @@ async fn cmd_delete_all_grpc_connections<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<()> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_all_grpc_connections_for_request(request_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1723,16 +1580,13 @@ async fn cmd_delete_send_history<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<()> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.with_tx(|tx| {
|
||||
let source = &UpdateSource::from_window(&window);
|
||||
tx.delete_all_http_responses_for_workspace(workspace_id, source)?;
|
||||
tx.delete_all_grpc_connections_for_workspace(workspace_id, source)?;
|
||||
tx.delete_all_websocket_connections_for_workspace(workspace_id, source)?;
|
||||
Ok(())
|
||||
})
|
||||
.await?)
|
||||
Ok(app_handle.with_tx(|tx| {
|
||||
let source = &UpdateSource::from_window(&window);
|
||||
tx.delete_all_http_responses_for_workspace(workspace_id, source)?;
|
||||
tx.delete_all_grpc_connections_for_workspace(workspace_id, source)?;
|
||||
tx.delete_all_websocket_connections_for_workspace(workspace_id, source)?;
|
||||
Ok(())
|
||||
})?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1742,9 +1596,7 @@ async fn cmd_delete_all_http_responses<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<()> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_all_http_responses_for_request(request_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -1753,7 +1605,7 @@ async fn cmd_list_workspaces<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> YaakResult<Vec<Workspace>> {
|
||||
let queries = app_handle.queries().connect().await?;
|
||||
let queries = app_handle.db();
|
||||
let workspaces = queries.find_all::<Workspace>()?;
|
||||
if workspaces.is_empty() {
|
||||
let workspace = queries.upsert_workspace(
|
||||
@@ -1777,7 +1629,7 @@ async fn cmd_get_workspace_meta<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
workspace_id: &str,
|
||||
) -> YaakResult<WorkspaceMeta> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let workspace = db.get_workspace(workspace_id)?;
|
||||
Ok(db.get_or_create_workspace_meta(&workspace, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
@@ -1806,11 +1658,7 @@ async fn cmd_delete_workspace<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
workspace_id: &str,
|
||||
) -> YaakResult<Workspace> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.delete_workspace_by_id(workspace_id, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().delete_workspace_by_id(workspace_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -1928,13 +1776,10 @@ pub fn run() {
|
||||
cmd_export_data,
|
||||
cmd_filter_response,
|
||||
cmd_format_json,
|
||||
cmd_get_cookie_jar,
|
||||
cmd_get_environment,
|
||||
cmd_get_folder,
|
||||
cmd_get_grpc_request,
|
||||
cmd_get_http_authentication_summaries,
|
||||
cmd_get_http_authentication_config,
|
||||
cmd_get_http_request,
|
||||
cmd_get_key_value,
|
||||
cmd_get_settings,
|
||||
cmd_get_sse_events,
|
||||
@@ -1996,7 +1841,7 @@ pub fn run() {
|
||||
// Cancel pending requests
|
||||
let h = app_handle.clone();
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let db = h.queries().connect().await.unwrap();
|
||||
let db = h.db();
|
||||
let _ = db.cancel_pending_http_responses();
|
||||
let _ = db.cancel_pending_grpc_connections();
|
||||
let _ = db.cancel_pending_websocket_connections();
|
||||
@@ -2046,11 +1891,7 @@ pub fn run() {
|
||||
}
|
||||
|
||||
async fn get_update_mode<R: Runtime>(window: &WebviewWindow<R>) -> YaakResult<UpdateMode> {
|
||||
let settings = window
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_or_create_settings(&UpdateSource::from_window(window))?;
|
||||
let settings = window.db().get_or_create_settings(&UpdateSource::from_window(window));
|
||||
Ok(UpdateMode::new(settings.update_channel.as_str()))
|
||||
}
|
||||
|
||||
@@ -2152,7 +1993,7 @@ fn workspace_id_from_window<R: Runtime>(window: &WebviewWindow<R>) -> Option<Str
|
||||
async fn workspace_from_window<R: Runtime>(window: &WebviewWindow<R>) -> YaakResult<Workspace> {
|
||||
match workspace_id_from_window(&window) {
|
||||
None => Err(GenericError("Failed to get workspace ID from window".to_string())),
|
||||
Some(id) => Ok(window.queries().connect().await?.get_workspace(id.as_str())?),
|
||||
Some(id) => Ok(window.db().get_workspace(id.as_str())?),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2165,7 +2006,7 @@ fn environment_id_from_window<R: Runtime>(window: &WebviewWindow<R>) -> Option<S
|
||||
async fn environment_from_window<R: Runtime>(window: &WebviewWindow<R>) -> Option<Environment> {
|
||||
match environment_id_from_window(&window) {
|
||||
None => None,
|
||||
Some(id) => window.queries().connect().await.unwrap().get_environment(&id).ok(),
|
||||
Some(id) => window.db().get_environment(&id).ok(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2178,6 +2019,6 @@ fn cookie_jar_id_from_window<R: Runtime>(window: &WebviewWindow<R>) -> Option<St
|
||||
async fn cookie_jar_from_window<R: Runtime>(window: &WebviewWindow<R>) -> Option<CookieJar> {
|
||||
match cookie_jar_id_from_window(&window) {
|
||||
None => None,
|
||||
Some(id) => window.queries().connect().await.unwrap().get_cookie_jar(&id).ok(),
|
||||
Some(id) => window.db().get_cookie_jar(&id).ok(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ use reqwest::Method;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewWindow};
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::UpdateSource;
|
||||
|
||||
// Check for updates every hour
|
||||
const MAX_UPDATE_CHECK_SECONDS: u64 = 60 * 60;
|
||||
@@ -54,7 +54,7 @@ impl YaakNotifier {
|
||||
seen.push(id.to_string());
|
||||
debug!("Marked notification as seen {}", id);
|
||||
let seen_json = serde_json::to_string(&seen).map_err(|e| e.to_string())?;
|
||||
window.queries().connect().await.map_err(|e| e.to_string())?.set_key_value_raw(
|
||||
window.db().set_key_value_raw(
|
||||
KV_NAMESPACE,
|
||||
KV_KEY,
|
||||
seen_json.as_str(),
|
||||
@@ -118,13 +118,7 @@ impl YaakNotifier {
|
||||
}
|
||||
|
||||
async fn get_kv<R: Runtime>(app_handle: &AppHandle<R>) -> Result<Vec<String>, String> {
|
||||
match app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?
|
||||
.get_key_value_raw("notifications", "seen")
|
||||
{
|
||||
match app_handle.db().get_key_value_raw("notifications", "seen") {
|
||||
None => Ok(Vec::new()),
|
||||
Some(v) => serde_json::from_str(&v.value).map_err(|e| e.to_string()),
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ use chrono::Utc;
|
||||
use log::warn;
|
||||
use tauri::{AppHandle, Emitter, Manager, Runtime, State};
|
||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::models::{HttpResponse, Plugin};
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::UpdateSource;
|
||||
use yaak_plugins::events::{
|
||||
Color, DeleteKeyValueResponse, EmptyPayload, FindHttpResponsesResponse,
|
||||
GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent, InternalEventPayload,
|
||||
@@ -53,10 +53,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
}
|
||||
InternalEventPayload::FindHttpResponsesRequest(req) => {
|
||||
let http_responses = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.list_http_responses_for_request(
|
||||
req.request_id.as_str(),
|
||||
req.limit.map(|l| l as u64),
|
||||
@@ -67,13 +64,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
}))
|
||||
}
|
||||
InternalEventPayload::GetHttpRequestByIdRequest(req) => {
|
||||
let http_request = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.get_http_request(req.id.as_str())
|
||||
.unwrap();
|
||||
let http_request = app_handle.db().get_http_request(req.id.as_str()).ok();
|
||||
Some(InternalEventPayload::GetHttpRequestByIdResponse(GetHttpRequestByIdResponse {
|
||||
http_request,
|
||||
}))
|
||||
@@ -87,10 +78,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
.expect("Failed to get workspace_id from window URL");
|
||||
let environment = environment_from_window(&window).await;
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.get_base_environment(&workspace.id)
|
||||
.expect("Failed to get base environment");
|
||||
let cb = PluginTemplateCallback::new(app_handle, &window_context, req.purpose);
|
||||
@@ -115,10 +103,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
.expect("Failed to get workspace_id from window URL");
|
||||
let environment = environment_from_window(&window).await;
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.get_base_environment(&workspace.id)
|
||||
.expect("Failed to get base environment");
|
||||
let cb = PluginTemplateCallback::new(app_handle, &window_context, req.purpose);
|
||||
@@ -149,7 +134,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
InternalEventPayload::ReloadResponse(_) => {
|
||||
let window = get_window_from_window_context(app_handle, &window_context)
|
||||
.expect("Failed to find window for plugin reload");
|
||||
let plugins = app_handle.queries().connect().await.unwrap().list_plugins().unwrap();
|
||||
let plugins = app_handle.db().list_plugins().unwrap();
|
||||
for plugin in plugins {
|
||||
if plugin.directory != plugin_handle.dir {
|
||||
continue;
|
||||
@@ -159,13 +144,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
updated_at: Utc::now().naive_utc(), // TODO: Add reloaded_at field to use instead
|
||||
..plugin
|
||||
};
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.upsert_plugin(&new_plugin, &UpdateSource::Plugin)
|
||||
.unwrap();
|
||||
app_handle.db().upsert_plugin(&new_plugin, &UpdateSource::Plugin).unwrap();
|
||||
}
|
||||
let toast_event = plugin_handle.build_event_to_send(
|
||||
&WindowContext::from_window(&window),
|
||||
@@ -197,10 +176,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
HttpResponse::default()
|
||||
} else {
|
||||
window
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_http_response(
|
||||
&HttpResponse {
|
||||
request_id: http_request.id.clone(),
|
||||
@@ -292,34 +268,17 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
|
||||
}
|
||||
InternalEventPayload::SetKeyValueRequest(req) => {
|
||||
let name = plugin_handle.name().await;
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.set_plugin_key_value(&name, &req.key, &req.value);
|
||||
app_handle.db().set_plugin_key_value(&name, &req.key, &req.value);
|
||||
Some(InternalEventPayload::SetKeyValueResponse(SetKeyValueResponse {}))
|
||||
}
|
||||
InternalEventPayload::GetKeyValueRequest(req) => {
|
||||
let name = plugin_handle.name().await;
|
||||
let value = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.get_plugin_key_value(&name, &req.key)
|
||||
.map(|v| v.value);
|
||||
let value = app_handle.db().get_plugin_key_value(&name, &req.key).map(|v| v.value);
|
||||
Some(InternalEventPayload::GetKeyValueResponse(GetKeyValueResponse { value }))
|
||||
}
|
||||
InternalEventPayload::DeleteKeyValueRequest(req) => {
|
||||
let name = plugin_handle.name().await;
|
||||
let deleted = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.delete_plugin_key_value(&name, &req.key)
|
||||
.unwrap();
|
||||
let deleted = app_handle.db().delete_plugin_key_value(&name, &req.key).unwrap();
|
||||
Some(InternalEventPayload::DeleteKeyValueResponse(DeleteKeyValueResponse { deleted }))
|
||||
}
|
||||
_ => None,
|
||||
|
||||
@@ -7,8 +7,8 @@ use tauri::{Manager, Runtime, WebviewWindow};
|
||||
use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
|
||||
use tauri_plugin_updater::UpdaterExt;
|
||||
use tokio::task::block_in_place;
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::UpdateSource;
|
||||
use yaak_plugins::manager::PluginManager;
|
||||
|
||||
use crate::is_dev;
|
||||
@@ -67,11 +67,7 @@ impl YaakUpdater {
|
||||
mode: UpdateMode,
|
||||
update_trigger: UpdateTrigger,
|
||||
) -> Result<bool> {
|
||||
let settings = window
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_or_create_settings(&UpdateSource::from_window(window))?;
|
||||
let settings = window.db().get_or_create_settings(&UpdateSource::from_window(window));
|
||||
let update_key = format!("{:x}", md5::compute(settings.id));
|
||||
self.last_update_check = SystemTime::now();
|
||||
|
||||
|
||||
@@ -7,8 +7,8 @@ use std::ops::Add;
|
||||
use std::time::Duration;
|
||||
use tauri::{is_dev, AppHandle, Emitter, Manager, Runtime, WebviewWindow};
|
||||
use ts_rs::TS;
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::UpdateSource;
|
||||
|
||||
const KV_NAMESPACE: &str = "license";
|
||||
const KV_ACTIVATION_ID_KEY: &str = "activation_id";
|
||||
@@ -81,7 +81,7 @@ pub async fn activate_license<R: Runtime>(
|
||||
}
|
||||
|
||||
let body: ActivateLicenseResponsePayload = response.json().await?;
|
||||
window.app_handle().queries().connect().await?.set_key_value_string(
|
||||
window.app_handle().db().set_key_value_string(
|
||||
KV_ACTIVATION_ID_KEY,
|
||||
KV_NAMESPACE,
|
||||
body.activation_id.as_str(),
|
||||
@@ -118,7 +118,7 @@ pub async fn deactivate_license<R: Runtime>(
|
||||
return Err(ServerError);
|
||||
}
|
||||
|
||||
app_handle.queries().connect().await?.delete_key_value(
|
||||
app_handle.db().delete_key_value(
|
||||
KV_ACTIVATION_ID_KEY,
|
||||
KV_NAMESPACE,
|
||||
&UpdateSource::from_window(&window),
|
||||
@@ -146,11 +146,7 @@ pub async fn check_license<R: Runtime>(
|
||||
payload: CheckActivationRequestPayload,
|
||||
) -> Result<LicenseCheckStatus> {
|
||||
let activation_id = get_activation_id(window.app_handle()).await;
|
||||
let settings = window
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_or_create_settings(&UpdateSource::from_window(window))?;
|
||||
let settings = window.db().get_or_create_settings(&UpdateSource::from_window(window));
|
||||
let trial_end = settings.created_at.add(Duration::from_secs(TRIAL_SECONDS));
|
||||
|
||||
debug!("Trial ending at {trial_end:?}");
|
||||
@@ -201,7 +197,7 @@ fn build_url(path: &str) -> String {
|
||||
}
|
||||
|
||||
pub async fn get_activation_id<R: Runtime>(app_handle: &AppHandle<R>) -> String {
|
||||
app_handle.queries().connect().await.unwrap().get_key_value_string(
|
||||
app_handle.db().get_key_value_string(
|
||||
KV_ACTIVATION_ID_KEY,
|
||||
KV_NAMESPACE,
|
||||
"",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::QueryManagerExt;
|
||||
use crate::query_manager::QueryManagerExt;
|
||||
use crate::models::AnyModel;
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use tauri::{Runtime, WebviewWindow};
|
||||
|
||||
#[tauri::command]
|
||||
@@ -9,9 +9,8 @@ pub(crate) async fn upsert<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
model: AnyModel,
|
||||
) -> Result<String> {
|
||||
let queries = window.queries().connect().await?;
|
||||
let id = match model {
|
||||
AnyModel::HttpRequest(r) => queries.upsert(&r, &UpdateSource::from_window(&window))?.id,
|
||||
AnyModel::HttpRequest(r) => window.db().upsert(&r, &UpdateSource::from_window(&window))?.id,
|
||||
_ => todo!(),
|
||||
};
|
||||
|
||||
|
||||
25
src-tauri/yaak-models/src/connection_or_tx.rs
Normal file
25
src-tauri/yaak-models/src/connection_or_tx.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use r2d2::PooledConnection;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::{Connection, Statement, ToSql, Transaction};
|
||||
|
||||
pub enum ConnectionOrTx<'a> {
|
||||
Connection(PooledConnection<SqliteConnectionManager>),
|
||||
Transaction(&'a Transaction<'a>),
|
||||
}
|
||||
|
||||
impl<'a> ConnectionOrTx<'a> {
|
||||
pub(crate) fn resolve(&self) -> &Connection {
|
||||
match self {
|
||||
ConnectionOrTx::Connection(c) => c,
|
||||
ConnectionOrTx::Transaction(c) => c,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn prepare(&self, sql: &str) -> rusqlite::Result<Statement<'_>> {
|
||||
self.resolve().prepare(sql)
|
||||
}
|
||||
|
||||
pub(crate) fn execute(&self, sql: &str, params: &[&dyn ToSql]) -> rusqlite::Result<usize> {
|
||||
self.resolve().execute(sql, params)
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,40 @@
|
||||
use crate::connection_or_tx::ConnectionOrTx;
|
||||
use crate::error::Error::RowNotFound;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{AnyModel, ModelType, UpsertModelInfo};
|
||||
use crate::queries_legacy::{generate_model_id, ModelChangeEvent, ModelPayload, UpdateSource};
|
||||
use crate::util::{generate_model_id, ModelChangeEvent, ModelPayload, UpdateSource};
|
||||
use rusqlite::OptionalExtension;
|
||||
use sea_query::{
|
||||
Asterisk, Expr, IntoColumnRef, IntoIden, IntoTableRef, OnConflict, Query, SimpleExpr,
|
||||
SqliteQueryBuilder,
|
||||
};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub(crate) const MAX_HISTORY_ITEMS: usize = 20;
|
||||
pub struct DbContext<'a> {
|
||||
pub(crate) tx: mpsc::Sender<ModelPayload>,
|
||||
pub(crate) conn: ConnectionOrTx<'a>,
|
||||
}
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub(crate) fn find_one<'s, M>(
|
||||
&self,
|
||||
col: impl IntoColumnRef,
|
||||
value: impl Into<SimpleExpr>,
|
||||
) -> Result<M>
|
||||
) -> crate::error::Result<M>
|
||||
where
|
||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||
{
|
||||
match self.find_optional::<M>(col, value) {
|
||||
Ok(Some(v)) => Ok(v),
|
||||
Ok(None) => Err(RowNotFound),
|
||||
Err(e) => Err(e),
|
||||
Some(v) => Ok(v),
|
||||
None => Err(RowNotFound),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_optional<'s, M>(
|
||||
pub(crate) fn find_optional<'s, M>(
|
||||
&self,
|
||||
col: impl IntoColumnRef,
|
||||
value: impl Into<SimpleExpr>,
|
||||
) -> Result<Option<M>>
|
||||
) -> Option<M>
|
||||
where
|
||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||
{
|
||||
@@ -41,11 +43,13 @@ impl<'a> DbContext<'a> {
|
||||
.column(Asterisk)
|
||||
.cond_where(Expr::col(col).eq(value))
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
let mut stmt = self.conn.prepare(sql.as_str())?;
|
||||
Ok(stmt.query_row(&*params.as_params(), M::from_row).optional()?)
|
||||
let mut stmt = self.conn.prepare(sql.as_str()).expect("Failed to prepare query");
|
||||
stmt.query_row(&*params.as_params(), M::from_row)
|
||||
.optional()
|
||||
.expect("Failed to run find on DB")
|
||||
}
|
||||
|
||||
pub fn find_all<'s, M>(&self) -> Result<Vec<M>>
|
||||
pub fn find_all<'s, M>(&self) -> crate::error::Result<Vec<M>>
|
||||
where
|
||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||
{
|
||||
@@ -63,7 +67,7 @@ impl<'a> DbContext<'a> {
|
||||
col: impl IntoColumnRef,
|
||||
value: impl Into<SimpleExpr>,
|
||||
limit: Option<u64>,
|
||||
) -> Result<Vec<M>>
|
||||
) -> crate::error::Result<Vec<M>>
|
||||
where
|
||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||
{
|
||||
@@ -88,7 +92,7 @@ impl<'a> DbContext<'a> {
|
||||
Ok(items.map(|v| v.unwrap()).collect())
|
||||
}
|
||||
|
||||
pub fn upsert<M>(&self, model: &M, source: &UpdateSource) -> Result<M>
|
||||
pub fn upsert<M>(&self, model: &M, source: &UpdateSource) -> crate::error::Result<M>
|
||||
where
|
||||
M: Into<AnyModel> + From<AnyModel> + UpsertModelInfo + Clone,
|
||||
{
|
||||
@@ -112,7 +116,7 @@ impl<'a> DbContext<'a> {
|
||||
other_values: Vec<(impl IntoIden + Eq, impl Into<SimpleExpr>)>,
|
||||
update_columns: Vec<impl IntoIden>,
|
||||
source: &UpdateSource,
|
||||
) -> Result<M>
|
||||
) -> crate::error::Result<M>
|
||||
where
|
||||
M: Into<AnyModel> + From<AnyModel> + UpsertModelInfo + Clone,
|
||||
{
|
||||
@@ -147,7 +151,11 @@ impl<'a> DbContext<'a> {
|
||||
Ok(m)
|
||||
}
|
||||
|
||||
pub(crate) fn delete<'s, M>(&self, m: &M, update_source: &UpdateSource) -> Result<M>
|
||||
pub(crate) fn delete<'s, M>(
|
||||
&self,
|
||||
m: &M,
|
||||
update_source: &UpdateSource,
|
||||
) -> crate::error::Result<M>
|
||||
where
|
||||
M: Into<AnyModel> + Clone + UpsertModelInfo,
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::commands::{delete, upsert};
|
||||
use crate::manager::QueryManager;
|
||||
use crate::queries_legacy::ModelChangeEvent;
|
||||
use crate::query_manager::QueryManager;
|
||||
use crate::util::ModelChangeEvent;
|
||||
use log::info;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
@@ -19,11 +19,13 @@ use tokio::sync::mpsc;
|
||||
|
||||
mod commands;
|
||||
|
||||
mod connection_or_tx;
|
||||
mod db_context;
|
||||
pub mod error;
|
||||
pub mod manager;
|
||||
pub mod query_manager;
|
||||
pub mod models;
|
||||
pub mod queries;
|
||||
pub mod queries_legacy;
|
||||
pub mod util;
|
||||
pub mod render;
|
||||
|
||||
pub struct SqliteConnection(pub Mutex<Pool<SqliteConnectionManager>>);
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
use crate::error::Result;
|
||||
use crate::queries_legacy::ModelPayload;
|
||||
use r2d2::{Pool, PooledConnection};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::{Connection, Statement, ToSql, Transaction, TransactionBehavior};
|
||||
use std::sync::Arc;
|
||||
use tauri::{Manager, Runtime};
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
|
||||
pub trait QueryManagerExt<'a, R> {
|
||||
fn queries(&'a self) -> &'a QueryManager;
|
||||
}
|
||||
|
||||
impl<'a, R: Runtime, T: Manager<R>> QueryManagerExt<'a, R> for T {
|
||||
fn queries(&'a self) -> &'a QueryManager {
|
||||
let qm = self.state::<QueryManager>();
|
||||
qm.inner()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct QueryManager {
|
||||
pool: Arc<Mutex<Pool<SqliteConnectionManager>>>,
|
||||
events_tx: mpsc::Sender<ModelPayload>,
|
||||
}
|
||||
|
||||
impl QueryManager {
|
||||
pub(crate) fn new(
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
events_tx: mpsc::Sender<ModelPayload>,
|
||||
) -> Self {
|
||||
QueryManager {
|
||||
pool: Arc::new(Mutex::new(pool)),
|
||||
events_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn connect(&self) -> Result<DbContext> {
|
||||
let conn = self.pool.lock().await.get()?;
|
||||
Ok(DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Connection(conn),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn with_conn<F, T>(&self, func: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&DbContext) -> Result<T>,
|
||||
{
|
||||
let conn = self.pool.lock().await.get()?;
|
||||
let db_context = DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Connection(conn),
|
||||
};
|
||||
func(&db_context)
|
||||
}
|
||||
|
||||
pub async fn with_tx<F, T>(&self, func: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&DbContext) -> Result<T>,
|
||||
{
|
||||
let mut conn = self.pool.lock().await.get()?;
|
||||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
|
||||
let db_context = DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Transaction(&tx),
|
||||
};
|
||||
|
||||
match func(&db_context) {
|
||||
Ok(val) => {
|
||||
tx.commit()?;
|
||||
Ok(val)
|
||||
}
|
||||
Err(e) => {
|
||||
tx.rollback()?;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum ConnectionOrTx<'a> {
|
||||
Connection(PooledConnection<SqliteConnectionManager>),
|
||||
Transaction(&'a Transaction<'a>),
|
||||
}
|
||||
|
||||
impl<'a> ConnectionOrTx<'a> {
|
||||
pub(crate) fn resolve(&self) -> &Connection {
|
||||
match self {
|
||||
ConnectionOrTx::Connection(c) => c,
|
||||
ConnectionOrTx::Transaction(c) => c,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn prepare(&self, sql: &str) -> rusqlite::Result<Statement<'_>> {
|
||||
self.resolve().prepare(sql)
|
||||
}
|
||||
|
||||
pub(crate) fn execute(&self, sql: &str, params: &[&dyn ToSql]) -> rusqlite::Result<usize> {
|
||||
self.resolve().execute(sql, params)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DbContext<'a> {
|
||||
pub(crate) tx: mpsc::Sender<ModelPayload>,
|
||||
pub(crate) conn: ConnectionOrTx<'a>,
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use crate::models::HttpRequestIden::{
|
||||
Authentication, AuthenticationType, Body, BodyType, CreatedAt, Description, FolderId, Headers,
|
||||
Method, Name, SortPriority, UpdatedAt, Url, UrlParameters, WorkspaceId,
|
||||
};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use rusqlite::Row;
|
||||
use sea_query::{enum_def, IntoIden, IntoTableRef, SimpleExpr};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{Environment, Folder, GrpcRequest, HttpRequest, WebsocketRequest, Workspace};
|
||||
use crate::queries_legacy::{BatchUpsertResult, UpdateSource};
|
||||
use crate::util::{BatchUpsertResult, UpdateSource};
|
||||
use log::info;
|
||||
use crate::db_context::DbContext;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn batch_upsert(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{CookieJar, CookieJarIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_cookie_jar(&self, id: &str) -> Result<CookieJar> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{Environment, EnvironmentIden, UpsertModelInfo};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use log::info;
|
||||
use sea_query::ColumnRef::Asterisk;
|
||||
use sea_query::{Cond, Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use crate::db_context::DbContext;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_environment(&self, id: &str) -> Result<Environment> {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{
|
||||
Folder, FolderIden, GrpcRequest, GrpcRequestIden, HttpRequest, HttpRequestIden,
|
||||
WebsocketRequest, WebsocketRequestIden,
|
||||
};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_folder(&self, id: &str) -> Result<Folder> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{GrpcConnection, GrpcConnectionIden, GrpcConnectionState};
|
||||
use crate::queries::base::MAX_HISTORY_ITEMS;
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use log::debug;
|
||||
use sea_query::{Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use crate::db_context::DbContext;
|
||||
use crate::queries::MAX_HISTORY_ITEMS;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_grpc_connection(&self, id: &str) -> Result<GrpcConnection> {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{GrpcEvent, GrpcEventIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_grpc_events(&self, id: &str) -> Result<GrpcEvent> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{GrpcRequest, GrpcRequestIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_grpc_request(&self, id: &str) -> Result<Option<GrpcRequest>> {
|
||||
self.find_optional(GrpcRequestIden::Id, id)
|
||||
pub fn get_grpc_request(&self, id: &str) -> Result<GrpcRequest> {
|
||||
self.find_one(GrpcRequestIden::Id, id)
|
||||
}
|
||||
|
||||
pub fn list_grpc_requests(&self, workspace_id: &str) -> Result<Vec<GrpcRequest>> {
|
||||
@@ -26,7 +26,7 @@ impl<'a> DbContext<'a> {
|
||||
id: &str,
|
||||
source: &UpdateSource,
|
||||
) -> Result<GrpcRequest> {
|
||||
let request = self.get_grpc_request(id)?.unwrap();
|
||||
let request = self.get_grpc_request(id)?;
|
||||
self.delete_grpc_request(&request, source)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{HttpRequest, HttpRequestIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_http_request(&self, id: &str) -> Result<Option<HttpRequest>> {
|
||||
self.find_optional(HttpRequestIden::Id, id)
|
||||
pub fn get_http_request(&self, id: &str) -> Result<HttpRequest> {
|
||||
self.find_one(HttpRequestIden::Id, id)
|
||||
}
|
||||
|
||||
pub fn list_http_requests(&self, workspace_id: &str) -> Result<Vec<HttpRequest>> {
|
||||
@@ -26,7 +26,7 @@ impl<'a> DbContext<'a> {
|
||||
id: &str,
|
||||
source: &UpdateSource,
|
||||
) -> Result<HttpRequest> {
|
||||
let http_request = self.get_http_request(id)?.unwrap();
|
||||
let http_request = self.get_http_request(id)?;
|
||||
self.delete_http_request(&http_request, source)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{HttpResponse, HttpResponseIden, HttpResponseState};
|
||||
use crate::queries::base::MAX_HISTORY_ITEMS;
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use log::{debug, error};
|
||||
use sea_query::{Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use std::fs;
|
||||
use crate::db_context::DbContext;
|
||||
use crate::queries::MAX_HISTORY_ITEMS;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_http_response(&self, id: &str) -> Result<HttpResponse> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{KeyValue, KeyValueIden};
|
||||
use crate::queries_legacy::{ModelChangeEvent, ModelPayload, UpdateSource};
|
||||
use crate::util::{ModelChangeEvent, ModelPayload, UpdateSource};
|
||||
use log::error;
|
||||
use sea_query::Keyword::CurrentTimestamp;
|
||||
use sea_query::{Asterisk, Cond, Expr, OnConflict, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use crate::db_context::DbContext;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn list_key_values_raw(&self) -> Result<Vec<KeyValue>> {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
mod base;
|
||||
mod batch;
|
||||
mod cookie_jars;
|
||||
mod environments;
|
||||
@@ -18,3 +17,5 @@ mod websocket_events;
|
||||
mod websocket_requests;
|
||||
mod workspace_metas;
|
||||
mod workspaces;
|
||||
|
||||
const MAX_HISTORY_ITEMS: usize = 20;
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{PluginKeyValue, PluginKeyValueIden};
|
||||
use sea_query::Keyword::CurrentTimestamp;
|
||||
use sea_query::{Asterisk, Cond, Expr, OnConflict, Query, SqliteQueryBuilder};
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{Plugin, PluginIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_plugin(&self, id: &str) -> Result<Plugin> {
|
||||
self.find_one(PluginIden::Id, id)
|
||||
}
|
||||
|
||||
|
||||
pub fn list_plugins(&self) -> Result<Vec<Plugin>> {
|
||||
self.find_all()
|
||||
}
|
||||
@@ -20,7 +20,7 @@ impl<'a> DbContext<'a> {
|
||||
let plugin = self.get_plugin(id)?;
|
||||
self.delete_plugin(&plugin, source)
|
||||
}
|
||||
|
||||
|
||||
pub fn upsert_plugin(&self, plugin: &Plugin, source: &UpdateSource) -> Result<Plugin> {
|
||||
self.upsert(plugin, source)
|
||||
}
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{Settings, SettingsIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_or_create_settings(&self, source: &UpdateSource) -> Result<Settings> {
|
||||
let id = "default";
|
||||
if let Some(s) = self.find_optional::<Settings>(SettingsIden::Id, id)? {
|
||||
return Ok(s);
|
||||
pub fn get_or_create_settings(&self, source: &UpdateSource) -> Settings {
|
||||
let id = "default".to_string();
|
||||
|
||||
if let Some(s) = self.find_optional::<Settings>(SettingsIden::Id, &id) {
|
||||
return s;
|
||||
};
|
||||
|
||||
self.upsert(
|
||||
&Settings {
|
||||
id: id.to_string(),
|
||||
id,
|
||||
..Default::default()
|
||||
},
|
||||
source,
|
||||
)
|
||||
.expect("Failed to upsert settings")
|
||||
}
|
||||
|
||||
pub fn upsert_settings(&self, settings: &Settings, source: &UpdateSource) -> Result<Settings> {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{SyncState, SyncStateIden, UpsertModelInfo};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
use sea_query::{Asterisk, Cond, Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use std::path::Path;
|
||||
use crate::db_context::DbContext;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_sync_state(&self, id: &str) -> Result<SyncState> {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{WebsocketConnection, WebsocketConnectionIden, WebsocketConnectionState};
|
||||
use crate::queries::base::MAX_HISTORY_ITEMS;
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::queries::MAX_HISTORY_ITEMS;
|
||||
use crate::util::UpdateSource;
|
||||
use log::debug;
|
||||
use sea_query::{Expr, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{
|
||||
WebsocketEvent,
|
||||
WebsocketEventIden,
|
||||
};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_websocket_event(&self, id: &str) -> Result<WebsocketEvent> {
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{WebsocketRequest, WebsocketRequestIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_websocket_request(&self, id: &str) -> Result<Option<WebsocketRequest>> {
|
||||
self.find_optional(WebsocketRequestIden::Id, id)
|
||||
pub fn get_websocket_request(&self, id: &str) -> Result<WebsocketRequest> {
|
||||
self.find_one(WebsocketRequestIden::Id, id)
|
||||
}
|
||||
|
||||
pub fn list_websocket_requests(&self, workspace_id: &str) -> Result<Vec<WebsocketRequest>> {
|
||||
@@ -26,7 +26,7 @@ impl<'a> DbContext<'a> {
|
||||
id: &str,
|
||||
source: &UpdateSource,
|
||||
) -> Result<WebsocketRequest> {
|
||||
let request = self.get_websocket_request(id)?.unwrap();
|
||||
let request = self.get_websocket_request(id)?;
|
||||
self.delete_websocket_request(&request, source)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{Workspace, WorkspaceMeta, WorkspaceMetaIden};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_workspace_meta(&self, workspace: &Workspace) -> Result<Option<WorkspaceMeta>> {
|
||||
pub fn get_workspace_meta(&self, workspace: &Workspace) -> Option<WorkspaceMeta> {
|
||||
self.find_optional(WorkspaceMetaIden::WorkspaceId, &workspace.id)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ impl<'a> DbContext<'a> {
|
||||
workspace: &Workspace,
|
||||
source: &UpdateSource,
|
||||
) -> Result<WorkspaceMeta> {
|
||||
let workspace_meta = self.get_workspace_meta(workspace)?;
|
||||
let workspace_meta = self.get_workspace_meta(workspace);
|
||||
if let Some(workspace_meta) = workspace_meta {
|
||||
return Ok(workspace_meta);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::manager::DbContext;
|
||||
use crate::models::{
|
||||
Folder, FolderIden, GrpcRequest, GrpcRequestIden, HttpRequest, HttpRequestIden,
|
||||
WebsocketRequest, WebsocketRequestIden, Workspace, WorkspaceIden,
|
||||
};
|
||||
use crate::queries_legacy::UpdateSource;
|
||||
use crate::util::UpdateSource;
|
||||
|
||||
impl<'a> DbContext<'a> {
|
||||
pub fn get_workspace(&self, id: &str) -> Result<Workspace> {
|
||||
|
||||
124
src-tauri/yaak-models/src/query_manager.rs
Normal file
124
src-tauri/yaak-models/src/query_manager.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use crate::connection_or_tx::ConnectionOrTx;
|
||||
use crate::db_context::DbContext;
|
||||
use crate::error::Result;
|
||||
use crate::util::ModelPayload;
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::TransactionBehavior;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tauri::{Manager, Runtime};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
pub trait QueryManagerExt<'a, R> {
|
||||
fn db(&'a self) -> DbContext<'a>;
|
||||
fn with_db<F, T>(&'a self, func: F) -> T
|
||||
where
|
||||
F: FnOnce(&DbContext) -> T;
|
||||
fn with_tx<F, T>(&'a self, func: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&DbContext) -> Result<T>;
|
||||
}
|
||||
|
||||
impl<'a, R: Runtime, M: Manager<R>> QueryManagerExt<'a, R> for M {
|
||||
fn db(&'a self) -> DbContext<'a> {
|
||||
let qm = self.state::<QueryManager>();
|
||||
qm.inner().connect_2()
|
||||
}
|
||||
|
||||
fn with_db<F, T>(&'a self, func: F) -> T
|
||||
where
|
||||
F: FnOnce(&DbContext) -> T,
|
||||
{
|
||||
let qm = self.state::<QueryManager>();
|
||||
qm.inner().with_conn(func)
|
||||
}
|
||||
|
||||
fn with_tx<F, T>(&'a self, func: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&DbContext) -> Result<T>,
|
||||
{
|
||||
let qm = self.state::<QueryManager>();
|
||||
qm.inner().with_tx(func)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct QueryManager {
|
||||
pool: Arc<Mutex<Pool<SqliteConnectionManager>>>,
|
||||
events_tx: mpsc::Sender<ModelPayload>,
|
||||
}
|
||||
|
||||
impl QueryManager {
|
||||
pub(crate) fn new(
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
events_tx: mpsc::Sender<ModelPayload>,
|
||||
) -> Self {
|
||||
QueryManager {
|
||||
pool: Arc::new(Mutex::new(pool)),
|
||||
events_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn connect_2(&self) -> DbContext {
|
||||
let conn = self
|
||||
.pool
|
||||
.lock()
|
||||
.expect("Failed to gain lock on DB")
|
||||
.get()
|
||||
.expect("Failed to get a new DB connection from the pool");
|
||||
DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Connection(conn),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_conn<F, T>(&self, func: F) -> T
|
||||
where
|
||||
F: FnOnce(&DbContext) -> T,
|
||||
{
|
||||
let conn = self
|
||||
.pool
|
||||
.lock()
|
||||
.expect("Failed to gain lock on DB for transaction")
|
||||
.get()
|
||||
.expect("Failed to get new DB connection from the pool");
|
||||
|
||||
let db_context = DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Connection(conn),
|
||||
};
|
||||
|
||||
func(&db_context)
|
||||
}
|
||||
|
||||
pub fn with_tx<F, T>(&self, func: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce(&DbContext) -> Result<T>,
|
||||
{
|
||||
let mut conn = self
|
||||
.pool
|
||||
.lock()
|
||||
.expect("Failed to gain lock on DB for transaction")
|
||||
.get()
|
||||
.expect("Failed to get new DB connection from the pool");
|
||||
let tx = conn
|
||||
.transaction_with_behavior(TransactionBehavior::Immediate)
|
||||
.expect("Failed to start DB transaction");
|
||||
|
||||
let db_context = DbContext {
|
||||
tx: self.events_tx.clone(),
|
||||
conn: ConnectionOrTx::Transaction(&tx),
|
||||
};
|
||||
|
||||
match func(&db_context) {
|
||||
Ok(val) => {
|
||||
tx.commit()?;
|
||||
Ok(val)
|
||||
}
|
||||
Err(e) => {
|
||||
tx.rollback()?;
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
use crate::error::Result;
|
||||
use crate::manager::QueryManagerExt;
|
||||
use crate::models::{AnyModel, Environment, Folder, GrpcRequest, HttpRequest, ModelType, WebsocketRequest, Workspace, WorkspaceIden};
|
||||
use crate::models::{
|
||||
AnyModel, Environment, Folder, GrpcRequest, HttpRequest, ModelType, WebsocketRequest,
|
||||
Workspace, WorkspaceIden,
|
||||
};
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use log::warn;
|
||||
use nanoid::nanoid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Listener, Runtime, WebviewWindow};
|
||||
use ts_rs::TS;
|
||||
use crate::query_manager::QueryManagerExt;
|
||||
|
||||
pub fn generate_model_id(model: ModelType) -> String {
|
||||
let id = generate_id();
|
||||
@@ -131,7 +134,7 @@ pub async fn get_workspace_export_resources<R: Runtime>(
|
||||
},
|
||||
};
|
||||
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
for workspace_id in workspace_ids {
|
||||
data.resources.workspaces.push(db.find_one(WorkspaceIden::Id, workspace_id)?);
|
||||
data.resources.environments.append(&mut db.list_environments(workspace_id)?);
|
||||
@@ -26,8 +26,8 @@ use tokio::fs::read_dir;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
use tokio::time::{timeout, Instant};
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::queries_legacy::generate_id;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::generate_id;
|
||||
use yaak_templates::error::Error::RenderError;
|
||||
use yaak_templates::error::Result as TemplateResult;
|
||||
|
||||
@@ -160,7 +160,7 @@ impl PluginManager {
|
||||
.collect();
|
||||
|
||||
let plugins =
|
||||
app_handle.queries().connect().await.unwrap().list_plugins().unwrap_or_default();
|
||||
app_handle.db().list_plugins().unwrap_or_default();
|
||||
let installed_plugin_dirs: Vec<PluginCandidate> = plugins
|
||||
.iter()
|
||||
.map(|p| PluginCandidate {
|
||||
|
||||
@@ -11,9 +11,9 @@ use tokio::fs;
|
||||
use tokio::fs::File;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use ts_rs::TS;
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::models::{SyncState, WorkspaceMeta};
|
||||
use yaak_models::queries_legacy::{get_workspace_export_resources, UpdateSource};
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::util::{get_workspace_export_resources, UpdateSource};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "camelCase", tag = "type")]
|
||||
@@ -112,9 +112,7 @@ pub(crate) async fn get_db_candidates<R: Runtime>(
|
||||
.map(|m| (m.id(), m))
|
||||
.collect();
|
||||
let sync_states: HashMap<_, _> = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.list_sync_states_for_workspace(workspace_id, sync_dir)?
|
||||
.into_iter()
|
||||
.map(|s| (s.model_id.clone(), s))
|
||||
@@ -440,27 +438,24 @@ pub(crate) async fn apply_sync_ops<R: Runtime>(
|
||||
});
|
||||
}
|
||||
|
||||
let upserted_models = app_handle
|
||||
.queries()
|
||||
.with_tx(|tx| {
|
||||
tx.batch_upsert(
|
||||
workspaces_to_upsert,
|
||||
environments_to_upsert,
|
||||
folders_to_upsert,
|
||||
http_requests_to_upsert,
|
||||
grpc_requests_to_upsert,
|
||||
websocket_requests_to_upsert,
|
||||
&UpdateSource::Sync,
|
||||
)
|
||||
})
|
||||
.await?;
|
||||
let upserted_models = app_handle.with_tx(|tx| {
|
||||
tx.batch_upsert(
|
||||
workspaces_to_upsert,
|
||||
environments_to_upsert,
|
||||
folders_to_upsert,
|
||||
http_requests_to_upsert,
|
||||
grpc_requests_to_upsert,
|
||||
websocket_requests_to_upsert,
|
||||
&UpdateSource::Sync,
|
||||
)
|
||||
})?;
|
||||
|
||||
// Ensure we creat WorkspaceMeta models for each new workspace, with the appropriate sync dir
|
||||
let sync_dir_string = sync_dir.to_string_lossy().to_string();
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
for workspace in upserted_models.workspaces {
|
||||
let r = match db.get_workspace_meta(&workspace) {
|
||||
Ok(Some(m)) => {
|
||||
Some(m) => {
|
||||
if m.setting_sync_dir == Some(sync_dir_string.clone()) {
|
||||
// We don't need to update if unchanged
|
||||
continue;
|
||||
@@ -473,7 +468,7 @@ pub(crate) async fn apply_sync_ops<R: Runtime>(
|
||||
&UpdateSource::Sync,
|
||||
)
|
||||
}
|
||||
Ok(None) => db.upsert_workspace_meta(
|
||||
None => db.upsert_workspace_meta(
|
||||
&WorkspaceMeta {
|
||||
workspace_id: workspace_id.to_string(),
|
||||
setting_sync_dir: Some(sync_dir.to_string_lossy().to_string()),
|
||||
@@ -481,7 +476,6 @@ pub(crate) async fn apply_sync_ops<R: Runtime>(
|
||||
},
|
||||
&UpdateSource::Sync,
|
||||
),
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
|
||||
if let Err(e) = r {
|
||||
@@ -531,7 +525,7 @@ pub(crate) async fn apply_sync_state_ops<R: Runtime>(
|
||||
flushed_at: Utc::now().naive_utc(),
|
||||
..Default::default()
|
||||
};
|
||||
app_handle.queries().connect().await?.upsert_sync_state(&sync_state)?;
|
||||
app_handle.db().upsert_sync_state(&sync_state)?;
|
||||
}
|
||||
SyncStateOp::Update {
|
||||
state: sync_state,
|
||||
@@ -545,10 +539,10 @@ pub(crate) async fn apply_sync_state_ops<R: Runtime>(
|
||||
flushed_at: Utc::now().naive_utc(),
|
||||
..sync_state
|
||||
};
|
||||
app_handle.queries().connect().await?.upsert_sync_state(&sync_state)?;
|
||||
app_handle.db().upsert_sync_state(&sync_state)?;
|
||||
}
|
||||
SyncStateOp::Delete { state } => {
|
||||
app_handle.queries().connect().await?.delete_sync_state(&state)?;
|
||||
app_handle.db().delete_sync_state(&state)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -561,7 +555,7 @@ fn derive_model_filename(m: &SyncModel) -> PathBuf {
|
||||
}
|
||||
|
||||
async fn delete_model<R: Runtime>(app_handle: &AppHandle<R>, model: &SyncModel) -> Result<()> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
match model {
|
||||
SyncModel::Workspace(m) => {
|
||||
db.delete_workspace(&m, &UpdateSource::Sync)?;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use crate::error::Error::GenericError;
|
||||
use crate::error::Result;
|
||||
use crate::manager::WebsocketManager;
|
||||
use crate::render::render_request;
|
||||
@@ -10,12 +9,12 @@ use tokio::sync::{mpsc, Mutex};
|
||||
use tokio_tungstenite::tungstenite::http::HeaderValue;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use yaak_http::apply_path_placeholders;
|
||||
use yaak_models::manager::QueryManagerExt;
|
||||
use yaak_models::query_manager::QueryManagerExt;
|
||||
use yaak_models::models::{
|
||||
HttpResponseHeader, WebsocketConnection, WebsocketConnectionState, WebsocketEvent,
|
||||
WebsocketEventType, WebsocketRequest,
|
||||
};
|
||||
use yaak_models::queries_legacy::UpdateSource;
|
||||
use yaak_models::util::UpdateSource;
|
||||
use yaak_plugins::events::{
|
||||
CallHttpAuthenticationRequest, HttpHeader, RenderPurpose, WindowContext,
|
||||
};
|
||||
@@ -28,11 +27,7 @@ pub(crate) async fn upsert_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> Result<WebsocketRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.upsert_websocket_request(&request, &UpdateSource::from_window(&window))?)
|
||||
Ok(app_handle.db().upsert_websocket_request(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -41,8 +36,8 @@ pub(crate) async fn duplicate_request<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> Result<WebsocketRequest> {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let request = db.get_websocket_request(request_id)?.unwrap();
|
||||
let db = app_handle.db();
|
||||
let request = db.get_websocket_request(request_id)?;
|
||||
Ok(db.duplicate_websocket_request(&request, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -53,9 +48,7 @@ pub(crate) async fn delete_request<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> Result<WebsocketRequest> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_websocket_request_by_id(request_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -66,9 +59,7 @@ pub(crate) async fn delete_connection<R: Runtime>(
|
||||
window: WebviewWindow<R>,
|
||||
) -> Result<WebsocketConnection> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.db()
|
||||
.delete_websocket_connection_by_id(connection_id, &UpdateSource::from_window(&window))?)
|
||||
}
|
||||
|
||||
@@ -78,7 +69,7 @@ pub(crate) async fn delete_connections<R: Runtime>(
|
||||
app_handle: AppHandle<R>,
|
||||
window: WebviewWindow<R>,
|
||||
) -> Result<()> {
|
||||
Ok(app_handle.queries().connect().await?.delete_all_websocket_connections_for_request(
|
||||
Ok(app_handle.db().delete_all_websocket_connections_for_request(
|
||||
request_id,
|
||||
&UpdateSource::from_window(&window),
|
||||
)?)
|
||||
@@ -89,7 +80,7 @@ pub(crate) async fn list_events<R: Runtime>(
|
||||
connection_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> Result<Vec<WebsocketEvent>> {
|
||||
Ok(app_handle.queries().connect().await?.list_websocket_events(connection_id)?)
|
||||
Ok(app_handle.db().list_websocket_events(connection_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -97,7 +88,7 @@ pub(crate) async fn list_requests<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> Result<Vec<WebsocketRequest>> {
|
||||
Ok(app_handle.queries().connect().await?.list_websocket_requests(workspace_id)?)
|
||||
Ok(app_handle.db().list_websocket_requests(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -105,11 +96,7 @@ pub(crate) async fn list_connections<R: Runtime>(
|
||||
workspace_id: &str,
|
||||
app_handle: AppHandle<R>,
|
||||
) -> Result<Vec<WebsocketConnection>> {
|
||||
Ok(app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.list_websocket_connections_for_workspace(workspace_id)?)
|
||||
Ok(app_handle.db().list_websocket_connections_for_workspace(workspace_id)?)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -121,22 +108,17 @@ pub(crate) async fn send<R: Runtime>(
|
||||
ws_manager: State<'_, Mutex<WebsocketManager>>,
|
||||
) -> Result<WebsocketConnection> {
|
||||
let (connection, unrendered_request) = {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let connection = db.get_websocket_connection(connection_id)?;
|
||||
let unrendered_request = db
|
||||
.get_websocket_request(&connection.request_id)?
|
||||
.ok_or(GenericError("WebSocket Request not found".to_string()))?;
|
||||
let unrendered_request = db.get_websocket_request(&connection.request_id)?;
|
||||
(connection, unrendered_request)
|
||||
};
|
||||
let environment = match environment_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_environment(id)?),
|
||||
Some(id) => Some(app_handle.db().get_environment(id)?),
|
||||
None => None,
|
||||
};
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let base_environment =
|
||||
app_handle.db().get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let request = render_request(
|
||||
&unrendered_request,
|
||||
&base_environment,
|
||||
@@ -152,7 +134,7 @@ pub(crate) async fn send<R: Runtime>(
|
||||
let mut ws_manager = ws_manager.lock().await;
|
||||
ws_manager.send(&connection.id, Message::Text(request.message.clone().into())).await?;
|
||||
|
||||
app_handle.queries().connect().await?.upsert_websocket_event(
|
||||
app_handle.db().upsert_websocket_event(
|
||||
&WebsocketEvent {
|
||||
connection_id: connection.id.clone(),
|
||||
request_id: request.id.clone(),
|
||||
@@ -176,7 +158,7 @@ pub(crate) async fn close<R: Runtime>(
|
||||
ws_manager: State<'_, Mutex<WebsocketManager>>,
|
||||
) -> Result<WebsocketConnection> {
|
||||
let connection = {
|
||||
let db = app_handle.queries().connect().await?;
|
||||
let db = app_handle.db();
|
||||
let connection = db.get_websocket_connection(connection_id)?;
|
||||
db.upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
@@ -205,21 +187,13 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
plugin_manager: State<'_, PluginManager>,
|
||||
ws_manager: State<'_, Mutex<WebsocketManager>>,
|
||||
) -> Result<WebsocketConnection> {
|
||||
let unrendered_request = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_websocket_request(request_id)?
|
||||
.ok_or(GenericError("Failed to find GRPC request".to_string()))?;
|
||||
let unrendered_request = app_handle.db().get_websocket_request(request_id)?;
|
||||
let environment = match environment_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_environment(id)?),
|
||||
Some(id) => Some(app_handle.db().get_environment(id)?),
|
||||
None => None,
|
||||
};
|
||||
let base_environment = app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await?
|
||||
.get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let base_environment =
|
||||
app_handle.db().get_base_environment(&unrendered_request.workspace_id)?;
|
||||
let request = render_request(
|
||||
&unrendered_request,
|
||||
&base_environment,
|
||||
@@ -262,11 +236,11 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
|
||||
// TODO: Handle cookies
|
||||
let _cookie_jar = match cookie_jar_id {
|
||||
Some(id) => Some(app_handle.queries().connect().await?.get_cookie_jar(id)?),
|
||||
Some(id) => Some(app_handle.db().get_cookie_jar(id)?),
|
||||
None => None,
|
||||
};
|
||||
|
||||
let connection = app_handle.queries().connect().await?.upsert_websocket_connection(
|
||||
let connection = app_handle.db().upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
request_id: request_id.to_string(),
|
||||
@@ -296,7 +270,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
return Ok(app_handle.queries().connect().await?.upsert_websocket_connection(
|
||||
return Ok(app_handle.db().upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
error: Some(format!("{e:?}")),
|
||||
state: WebsocketConnectionState::Closed,
|
||||
@@ -307,7 +281,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
}
|
||||
};
|
||||
|
||||
app_handle.queries().connect().await?.upsert_websocket_event(
|
||||
app_handle.db().upsert_websocket_event(
|
||||
&WebsocketEvent {
|
||||
connection_id: connection.id.clone(),
|
||||
request_id: request.id.clone(),
|
||||
@@ -328,7 +302,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
})
|
||||
.collect::<Vec<HttpResponseHeader>>();
|
||||
|
||||
let connection = app_handle.queries().connect().await?.upsert_websocket_connection(
|
||||
let connection = app_handle.db().upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
state: WebsocketConnectionState::Connected,
|
||||
headers: response_headers,
|
||||
@@ -352,10 +326,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
}
|
||||
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_websocket_event(
|
||||
&WebsocketEvent {
|
||||
connection_id: connection_id.clone(),
|
||||
@@ -381,10 +352,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
info!("Websocket connection closed");
|
||||
if !has_written_close {
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_websocket_event(
|
||||
&WebsocketEvent {
|
||||
connection_id: connection_id.clone(),
|
||||
@@ -399,10 +367,7 @@ pub(crate) async fn connect<R: Runtime>(
|
||||
.unwrap();
|
||||
}
|
||||
app_handle
|
||||
.queries()
|
||||
.connect()
|
||||
.await
|
||||
.unwrap()
|
||||
.db()
|
||||
.upsert_websocket_connection(
|
||||
&WebsocketConnection {
|
||||
workspace_id: request.workspace_id.clone(),
|
||||
|
||||
@@ -27,13 +27,10 @@ type TauriCmd =
|
||||
| 'cmd_export_data'
|
||||
| 'cmd_filter_response'
|
||||
| 'cmd_format_json'
|
||||
| 'cmd_get_cookie_jar'
|
||||
| 'cmd_get_environment'
|
||||
| 'cmd_get_folder'
|
||||
| 'cmd_get_grpc_request'
|
||||
| 'cmd_get_http_authentication_config'
|
||||
| 'cmd_get_http_authentication_summaries'
|
||||
| 'cmd_get_http_request'
|
||||
| 'cmd_get_key_value'
|
||||
| 'cmd_get_settings'
|
||||
| 'cmd_get_sse_events'
|
||||
|
||||
Reference in New Issue
Block a user