tauri_plugin_velesdb/
lib.rs

1//! # tauri-plugin-velesdb
2//!
3//! A Tauri plugin for `VelesDB` - Vector search in desktop applications.
4//!
5//! This plugin provides seamless integration of `VelesDB`'s vector database
6//! capabilities into Tauri desktop applications.
7//!
8//! ## Features
9//!
10//! - **Collection Management**: Create, list, and delete vector collections
11//! - **Vector Operations**: Insert, update, and delete vectors with payloads
12//! - **Vector Search**: Fast similarity search with multiple distance metrics
13//! - **Text Search**: BM25 full-text search across payloads
14//! - **Hybrid Search**: Combined vector + text search with RRF fusion
15//! - **`VelesQL`**: SQL-like query language for advanced searches
16//!
17//! ## Usage
18//!
19//! ### Rust (Plugin Registration)
20//!
21//! ```rust,ignore
22//! fn main() {
23//!     tauri::Builder::default()
24//!         .plugin(tauri_plugin_velesdb::init("./data"))
25//!         .run(tauri::generate_context!())
26//!         .expect("error while running tauri application");
27//! }
28//! ```
29//!
30//! ### JavaScript (Frontend)
31//!
32//! ```javascript
33//! import { invoke } from '@tauri-apps/api/core';
34//!
35//! // Create a collection
36//! await invoke('plugin:velesdb|create_collection', {
37//!   request: { name: 'documents', dimension: 768, metric: 'cosine' }
38//! });
39//!
40//! // Insert vectors
41//! await invoke('plugin:velesdb|upsert', {
42//!   request: {
43//!     collection: 'documents',
44//!     points: [{ id: 1, vector: [...], payload: { title: 'Doc' } }]
45//!   }
46//! });
47//!
48//! // Search
49//! const results = await invoke('plugin:velesdb|search', {
50//!   request: { collection: 'documents', vector: [...], topK: 10 }
51//! });
52//! ```
53
54#![warn(clippy::all, clippy::pedantic)]
55#![allow(clippy::module_name_repetitions)]
56
57use std::path::Path;
58
59use tauri::{
60    plugin::{Builder, TauriPlugin},
61    Manager, Runtime,
62};
63
64pub mod commands;
65pub mod error;
66pub mod state;
67
68pub use error::{CommandError, Error, Result};
69pub use state::VelesDbState;
70
71/// Initializes the `VelesDB` plugin with the specified data directory.
72///
73/// # Arguments
74///
75/// * `path` - Path to the database directory
76///
77/// # Returns
78///
79/// A configured `TauriPlugin` ready to be registered with the Tauri app.
80///
81/// # Example
82///
83/// ```rust,ignore
84/// tauri::Builder::default()
85///     .plugin(tauri_plugin_velesdb::init("./velesdb_data"))
86///     .run(tauri::generate_context!())
87///     .expect("error while running tauri application");
88/// ```
89#[must_use]
90pub fn init<R: Runtime, P: AsRef<Path>>(path: P) -> TauriPlugin<R> {
91    let db_path = path.as_ref().to_path_buf();
92
93    Builder::new("velesdb")
94        .invoke_handler(tauri::generate_handler![
95            commands::create_collection,
96            commands::delete_collection,
97            commands::list_collections,
98            commands::get_collection,
99            commands::upsert,
100            commands::get_points,
101            commands::delete_points,
102            commands::search,
103            commands::batch_search,
104            commands::text_search,
105            commands::hybrid_search,
106            commands::query,
107        ])
108        .setup(move |app, _api| {
109            let state = VelesDbState::new(db_path.clone());
110            app.manage(state);
111            tracing::info!("VelesDB plugin initialized with path: {:?}", db_path);
112            Ok(())
113        })
114        .build()
115}
116
117/// Initializes the `VelesDB` plugin with the default data directory.
118///
119/// Uses `./velesdb_data` as the default path.
120///
121/// # Returns
122///
123/// A configured `TauriPlugin` ready to be registered with the Tauri app.
124#[must_use]
125pub fn init_default<R: Runtime>() -> TauriPlugin<R> {
126    init("./velesdb_data")
127}
128
129#[cfg(test)]
130mod tests {
131    use super::*;
132
133    #[test]
134    fn test_velesdb_state_creation() {
135        // Arrange
136        let path = std::path::PathBuf::from("/tmp/test");
137
138        // Act
139        let state = VelesDbState::new(path.clone());
140
141        // Assert
142        assert_eq!(state.path(), &path);
143    }
144}