|
|
@ -14,8 +14,8 @@ use sqlx::{
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use tauri::{
|
|
|
|
use tauri::{
|
|
|
|
command,
|
|
|
|
command,
|
|
|
|
plugin::{Plugin, Result as PluginResult},
|
|
|
|
plugin::{Builder as PluginBuilder, TauriPlugin},
|
|
|
|
AppHandle, Invoke, Manager, RunEvent, Runtime, State,
|
|
|
|
AppHandle, Manager, RunEvent, Runtime, State,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
use tokio::sync::Mutex;
|
|
|
|
|
|
|
|
|
|
|
@ -92,7 +92,7 @@ struct DbInstances(Mutex<HashMap<String, Pool<Db>>>);
|
|
|
|
struct Migrations(Mutex<HashMap<String, MigrationList>>);
|
|
|
|
struct Migrations(Mutex<HashMap<String, MigrationList>>);
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
#[derive(Default, Deserialize)]
|
|
|
|
struct PluginConfig {
|
|
|
|
pub struct PluginConfig {
|
|
|
|
#[serde(default)]
|
|
|
|
#[serde(default)]
|
|
|
|
preload: Vec<String>,
|
|
|
|
preload: Vec<String>,
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -300,49 +300,31 @@ async fn select(
|
|
|
|
Ok(values)
|
|
|
|
Ok(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Tauri SQL plugin.
|
|
|
|
/// Tauri SQL plugin builder.
|
|
|
|
pub struct TauriSql<R: Runtime> {
|
|
|
|
pub struct Builder {
|
|
|
|
migrations: Option<HashMap<String, MigrationList>>,
|
|
|
|
migrations: Option<HashMap<String, MigrationList>>,
|
|
|
|
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<R: Runtime> Default for TauriSql<R> {
|
|
|
|
impl Builder {
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
|
|
migrations: Some(Default::default()),
|
|
|
|
|
|
|
|
invoke_handler: Box::new(tauri::generate_handler![load, execute, select, close]),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<R: Runtime> TauriSql<R> {
|
|
|
|
|
|
|
|
/// Add migrations to a database.
|
|
|
|
/// Add migrations to a database.
|
|
|
|
#[must_use]
|
|
|
|
#[must_use]
|
|
|
|
pub fn add_migrations(mut self, db_url: &str, migrations: Vec<Migration>) -> Self {
|
|
|
|
pub fn add_migrations(mut self, db_url: &str, migrations: Vec<Migration>) -> Self {
|
|
|
|
self.migrations
|
|
|
|
self.migrations
|
|
|
|
.as_mut()
|
|
|
|
.get_or_insert(Default::default())
|
|
|
|
.unwrap()
|
|
|
|
|
|
|
|
.insert(db_url.to_string(), MigrationList(migrations));
|
|
|
|
.insert(db_url.to_string(), MigrationList(migrations));
|
|
|
|
self
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<R: Runtime> Plugin<R> for TauriSql<R> {
|
|
|
|
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R, Option<PluginConfig>> {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
PluginBuilder::new("sql")
|
|
|
|
"sql"
|
|
|
|
.invoke_handler(tauri::generate_handler![load, execute, select, close])
|
|
|
|
}
|
|
|
|
.setup_with_config(|app, config: Option<PluginConfig>| {
|
|
|
|
|
|
|
|
let config = config.unwrap_or_default();
|
|
|
|
fn initialize(&mut self, app: &AppHandle<R>, config: serde_json::Value) -> PluginResult<()> {
|
|
|
|
|
|
|
|
tauri::async_runtime::block_on(async move {
|
|
|
|
|
|
|
|
let config: PluginConfig = if config.is_null() {
|
|
|
|
|
|
|
|
Default::default()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
serde_json::from_value(config)?
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
#[cfg(feature = "sqlite")]
|
|
|
|
create_dir_all(app_path(app)).expect("problems creating App directory!");
|
|
|
|
create_dir_all(app_path(app)).expect("problems creating App directory!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tauri::async_runtime::block_on(async move {
|
|
|
|
let instances = DbInstances::default();
|
|
|
|
let instances = DbInstances::default();
|
|
|
|
let mut lock = instances.0.lock().await;
|
|
|
|
let mut lock = instances.0.lock().await;
|
|
|
|
for db in config.preload {
|
|
|
|
for db in config.preload {
|
|
|
@ -363,17 +345,14 @@ impl<R: Runtime> Plugin<R> for TauriSql<R> {
|
|
|
|
lock.insert(db, pool);
|
|
|
|
lock.insert(db, pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(lock);
|
|
|
|
drop(lock);
|
|
|
|
|
|
|
|
|
|
|
|
app.manage(instances);
|
|
|
|
app.manage(instances);
|
|
|
|
app.manage(Migrations(Mutex::new(self.migrations.take().unwrap())));
|
|
|
|
app.manage(Migrations(Mutex::new(self.migrations.take().unwrap())));
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
.on_event(|app, event| {
|
|
|
|
fn extend_api(&mut self, message: Invoke<R>) {
|
|
|
|
|
|
|
|
(self.invoke_handler)(message)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
|
|
|
|
|
|
|
|
if let RunEvent::Exit = event {
|
|
|
|
if let RunEvent::Exit = event {
|
|
|
|
tauri::async_runtime::block_on(async move {
|
|
|
|
tauri::async_runtime::block_on(async move {
|
|
|
|
let instances = &*app.state::<DbInstances>();
|
|
|
|
let instances = &*app.state::<DbInstances>();
|
|
|
@ -383,5 +362,7 @@ impl<R: Runtime> Plugin<R> for TauriSql<R> {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|