mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-18 23:09:47 +02:00
Queries now use AppHandle instead of Window (#189)
This commit is contained in:
@@ -44,7 +44,7 @@ export type HttpUrlParameter = { enabled?: boolean, name: string, value: string,
|
||||
|
||||
export type KeyValue = { model: "key_value", createdAt: string, updatedAt: string, key: string, namespace: string, value: string, };
|
||||
|
||||
export type ModelPayload = { model: AnyModel, windowLabel: string, updateSource: UpdateSource, };
|
||||
export type ModelPayload = { model: AnyModel, updateSource: UpdateSource, };
|
||||
|
||||
export type Plugin = { model: "plugin", id: string, createdAt: string, updatedAt: string, checkedAt: string | null, directory: string, enabled: boolean, url: string | null, };
|
||||
|
||||
@@ -60,7 +60,7 @@ export type SyncHistory = { model: "sync_history", id: string, workspaceId: stri
|
||||
|
||||
export type SyncState = { model: "sync_state", id: string, workspaceId: string, createdAt: string, updatedAt: string, flushedAt: string, modelId: string, checksum: string, relPath: string, syncDir: string, };
|
||||
|
||||
export type UpdateSource = "sync" | "window" | "plugin" | "background" | "import";
|
||||
export type UpdateSource = { "type": "sync" } | { "type": "window", label: string, } | { "type": "plugin" } | { "type": "background" } | { "type": "import" };
|
||||
|
||||
export type WebsocketConnection = { model: "websocket_connection", id: string, createdAt: string, updatedAt: string, workspaceId: string, requestId: string, elapsed: number, error: string | null, headers: Array<HttpResponseHeader>, state: WebsocketConnectionState, status: number, url: string, };
|
||||
|
||||
|
||||
@@ -4,10 +4,16 @@ use thiserror::Error;
|
||||
pub enum Error {
|
||||
#[error("SQL error: {0}")]
|
||||
SqlError(#[from] rusqlite::Error),
|
||||
|
||||
#[error("SQL Pool error: {0}")]
|
||||
SqlPoolError(#[from] r2d2::Error),
|
||||
|
||||
#[error("JSON error: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
|
||||
#[error("Model not found {0}")]
|
||||
ModelNotFound(String),
|
||||
|
||||
#[error("unknown error")]
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -4,3 +4,4 @@ pub mod queries;
|
||||
|
||||
pub mod plugin;
|
||||
pub mod render;
|
||||
pub mod manager;
|
||||
|
||||
40
src-tauri/yaak-models/src/manager.rs
Normal file
40
src-tauri/yaak-models/src/manager.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::error::Result;
|
||||
use crate::models::{Workspace, WorkspaceIden};
|
||||
use crate::plugin::SqliteConnection;
|
||||
use r2d2::{Pool, PooledConnection};
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::Connection;
|
||||
use sea_query::{Asterisk, Order, Query, SqliteQueryBuilder};
|
||||
use sea_query_rusqlite::RusqliteBinder;
|
||||
use std::future::Future;
|
||||
use std::ops::Deref;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
|
||||
pub struct QueryManager {
|
||||
pool: Pool<SqliteConnectionManager>,
|
||||
}
|
||||
|
||||
pub trait DBConnection {
|
||||
fn connect(
|
||||
&self,
|
||||
) -> impl Future<Output = Result<PooledConnection<SqliteConnectionManager>>> + Send;
|
||||
}
|
||||
|
||||
impl<R: Runtime> DBConnection for AppHandle<R> {
|
||||
async fn connect(&self) -> Result<PooledConnection<SqliteConnectionManager>> {
|
||||
let dbm = &*self.state::<SqliteConnection>();
|
||||
let db = dbm.0.lock().await.get()?;
|
||||
Ok(db)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn list_workspaces<T: Deref<Target = Connection>>(c: &T) -> Result<Vec<Workspace>> {
|
||||
let (sql, params) = Query::select()
|
||||
.from(WorkspaceIden::Table)
|
||||
.column(Asterisk)
|
||||
.order_by(WorkspaceIden::Name, Order::Asc)
|
||||
.build_rusqlite(SqliteQueryBuilder);
|
||||
let mut stmt = c.prepare(sql.as_str())?;
|
||||
let items = stmt.query_map(&*params.as_params(), |row| row.try_into())?;
|
||||
Ok(items.map(|v| v.unwrap()).collect())
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user