use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
mod commands;
pub mod db;
pub mod error;
#[cfg(feature = "geo")]
pub mod geo;
pub mod sync;
pub use commands::*;
pub use error::*;
use std::sync::Arc;
use tokio::sync::RwLock;
use sync::SyncEngine;
pub struct PgSyncState {
pub engine: Arc<RwLock<Option<SyncEngine>>>,
}
impl Default for PgSyncState {
fn default() -> Self {
Self {
engine: Arc::new(RwLock::new(None)),
}
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("pg-sync")
.setup(|app, _api| {
app.manage(PgSyncState::default());
log::info!("[PgSync] Plugin initialized");
Ok(())
})
.invoke_handler(tauri::generate_handler![
commands::init_database,
commands::init_database_mobile,
commands::get_db_path,
commands::connect_remote,
commands::connect_remote_with_retry,
commands::connect_remote_quick,
commands::start_auto_reconnect,
commands::disconnect_remote,
commands::sync_now,
commands::is_online,
commands::start_realtime_listener,
commands::set_sync_filter,
commands::remove_sync_filter,
commands::get_sync_filter,
commands::get_all_sync_filters,
commands::ensure_table,
commands::insert,
commands::update,
commands::delete,
commands::find_by_id,
commands::find_all,
commands::find_where,
commands::query,
commands::count,
commands::insert_many,
commands::update_many,
commands::delete_many,
commands::clear_table,
commands::get_local_schema,
commands::list_local_tables,
commands::list_remote_tables,
commands::push_table_schema,
commands::pull_table_schema,
commands::get_registered_schema,
commands::list_registered_tables,
commands::purge_deleted,
commands::purge_all_deleted,
commands::get_deleted_stats,
])
.build()
}
pub trait PgSyncExt<R: Runtime> {
fn pg_sync_state(&self) -> tauri::State<'_, PgSyncState>;
}
impl<R: Runtime, T: Manager<R>> PgSyncExt<R> for T {
fn pg_sync_state(&self) -> tauri::State<'_, PgSyncState> {
self.state::<PgSyncState>()
}
}