lumosai_vector_postgres/lib.rs
1//! # Lumos Vector PostgreSQL
2//!
3//! PostgreSQL with pgvector implementation for the lumos-vector-core architecture.
4//!
5//! This crate provides a PostgreSQL backend with pgvector extension for vector storage,
6//! implementing the unified VectorStorage trait from lumos-vector-core.
7//!
8//! ## Features
9//!
10//! - **SQL Integration**: Leverages PostgreSQL's ACID properties
11//! - **pgvector Extension**: High-performance vector operations
12//! - **Rich Queries**: Complex SQL queries with vector similarity
13//! - **Transactions**: Full transaction support
14//! - **Indexing**: Multiple vector index types (IVFFlat, HNSW)
15//!
16//! ## Example
17//!
18//! ```rust
19//! use lumos_vector_postgres::PostgresVectorStorage;
20//! use lumos_vector_core::prelude::*;
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//! // Create PostgreSQL storage
25//! let storage = PostgresVectorStorage::new("postgresql://user:pass@localhost/db").await?;
26//!
27//! // Create an index
28//! let config = IndexConfig::new("documents", 384)
29//! .with_metric(SimilarityMetric::Cosine);
30//! storage.create_index(config).await?;
31//!
32//! // Insert documents
33//! let docs = vec![
34//! Document::new("doc1", "Hello world")
35//! .with_embedding(vec![0.1; 384])
36//! .with_metadata("type", "greeting"),
37//! ];
38//! storage.upsert_documents("documents", docs).await?;
39//!
40//! Ok(())
41//! }
42//! ```
43
44pub mod storage;
45pub mod config;
46pub mod error;
47
48pub use storage::PostgresVectorStorage;
49pub use config::PostgresConfig;
50pub use error::{PostgresError, PostgresResult};
51
52// Re-export core types for convenience
53pub use lumosai_vector_core::prelude::*;
54
55/// Create a new PostgreSQL vector storage instance
56pub async fn create_postgres_storage(database_url: &str) -> Result<PostgresVectorStorage> {
57 PostgresVectorStorage::new(database_url).await
58}
59
60/// Create a new PostgreSQL vector storage instance with configuration
61pub async fn create_postgres_storage_with_config(config: PostgresConfig) -> Result<PostgresVectorStorage> {
62 PostgresVectorStorage::with_config(config).await
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[tokio::test]
70 async fn test_create_storage() {
71 // This test requires a running PostgreSQL instance with pgvector
72 // Skip if not available
73 if std::env::var("DATABASE_URL").is_err() {
74 return;
75 }
76
77 let url = std::env::var("DATABASE_URL").unwrap();
78 let storage = create_postgres_storage(&url).await;
79
80 // Should either succeed or fail with connection error
81 match storage {
82 Ok(_) => println!("Successfully connected to PostgreSQL"),
83 Err(e) => println!("Failed to connect to PostgreSQL: {}", e),
84 }
85 }
86}