1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Connection pool for fraiseql-wire clients.
//!
//! fraiseql-wire's `FraiseClient` consumes itself on query execution,
//! so we implement a simple connection factory pattern instead of traditional pooling.
use std::sync::Arc;
use fraiseql_error::{FraiseQLError, Result};
/// Connection factory for fraiseql-wire clients.
///
/// Since `FraiseClient::query()` consumes the client, we store the connection string
/// and create new clients on demand rather than pooling connections.
#[derive(Debug, Clone)]
pub struct WireClientFactory {
connection_string: Arc<String>,
}
impl WireClientFactory {
/// Create a new client factory.
///
/// # Arguments
///
/// * `connection_string` - PostgreSQL connection string (e.g., "postgres://localhost/mydb")
///
/// # Example
///
/// ```ignore
/// use fraiseql_core::db::wire_pool::WireClientFactory;
///
/// let factory = WireClientFactory::new("postgres://localhost/fraiseql");
/// ```
#[must_use]
pub fn new(connection_string: impl Into<String>) -> Self {
Self {
connection_string: Arc::new(connection_string.into()),
}
}
/// Create a new fraiseql-wire client.
///
/// This method creates a fresh connection each time it's called.
/// The connection is closed when the client is dropped after query execution.
///
/// # Returns
///
/// A new `FraiseClient` ready for query execution.
///
/// # Errors
///
/// Returns `FraiseQLError::ConnectionPool` if connection fails.
///
/// # Example
///
/// ```ignore
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// use fraiseql_core::db::wire_pool::WireClientFactory;
///
/// let factory = WireClientFactory::new("postgres://localhost/fraiseql");
/// let client = factory.create_client().await?;
/// # Ok(())
/// # }
/// ```
pub async fn create_client(&self) -> Result<fraiseql_wire::FraiseClient> {
fraiseql_wire::FraiseClient::connect(&self.connection_string)
.await
.map_err(|e| FraiseQLError::ConnectionPool {
message: format!("Failed to create fraiseql-wire client: {e}"),
})
}
/// Get the connection string.
#[must_use]
pub fn connection_string(&self) -> &str {
&self.connection_string
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_factory_creation() {
let factory = WireClientFactory::new("postgres://localhost/test");
assert_eq!(factory.connection_string(), "postgres://localhost/test");
}
#[test]
fn test_factory_clone() {
let factory1 = WireClientFactory::new("postgres://localhost/test");
let factory2 = factory1.clone();
assert_eq!(factory1.connection_string(), factory2.connection_string());
}
}