pub struct Client { /* private fields */ }Expand description
Database client with connection pooling
This is the recommended way to use RustMemDB in applications. Similar to:
- PostgreSQL:
postgres::Client - MySQL:
mysql::Pool
§Examples
use rustmemodb::Client;
// Connect to database
let client = Client::connect("admin", "adminpass").await?;
// Execute queries
client.execute("CREATE TABLE users (id INTEGER, name TEXT, age INTEGER)").await?;
client.execute("INSERT INTO users VALUES (1, 'Alice', 30)").await?;
let result = client.query("SELECT * FROM users WHERE age > 25").await?;
println!("Found {} users", result.row_count());Implementations§
Source§impl Client
impl Client
Sourcepub async fn connect(username: &str, password: &str) -> Result<Self>
pub async fn connect(username: &str, password: &str) -> Result<Self>
Connect to database with username and password
Uses default configuration with connection pooling.
§Examples
let client = Client::connect("admin", "adminpass").await?;Sourcepub async fn connect_with_config(config: ConnectionConfig) -> Result<Self>
pub async fn connect_with_config(config: ConnectionConfig) -> Result<Self>
Connect with custom configuration
§Examples
let config = ConnectionConfig::new("admin", "admin")
.max_connections(20)
.database("mydb");
let client = Client::connect_with_config(config).await?;Sourcepub async fn connect_url(url: &str) -> Result<Self>
pub async fn connect_url(url: &str) -> Result<Self>
Connect using a connection string
Supports the following formats:
rustmemodb://username:password@host:port/databasepostgres://username:password@host:port/databasepostgresql://username:password@host:port/databasemysql://username:password@host:port/database
§Examples
let client = Client::connect_url(
"postgres://admin:adminpass@localhost:5432/mydb"
).await?;Sourcepub async fn connect_local(username: &str, password: &str) -> Result<Self>
pub async fn connect_local(username: &str, password: &str) -> Result<Self>
Create an isolated client with its own in-memory database instance.
This is ideal for unit tests, ensuring that parallel tests do not interfere with each other.
§Examples
let client = Client::connect_local("admin", "adminpass").await?;
// This client is completely isolated from other clientsSourcepub async fn query(&self, sql: &str) -> Result<QueryResult>
pub async fn query(&self, sql: &str) -> Result<QueryResult>
Execute a SQL query
§Examples
let result = client.query("SELECT * FROM users").await?;
for row in result.rows() {
println!("{:?}", row);
}Sourcepub async fn execute(&self, sql: &str) -> Result<QueryResult>
pub async fn execute(&self, sql: &str) -> Result<QueryResult>
Execute a statement (alias for query)
§Examples
client.execute("CREATE TABLE users (id INTEGER, name TEXT)").await?;
client.execute("INSERT INTO users VALUES (1, 'Alice')").await?;Sourcepub async fn get_connection(&self) -> Result<PoolGuard>
pub async fn get_connection(&self) -> Result<PoolGuard>
Get a connection from the pool for advanced usage
Use this when you need transaction support or multiple operations on the same connection.
§Examples
let mut conn = client.get_connection().await?;
conn.begin().await?;
conn.execute("INSERT INTO users VALUES (1, 'Alice', 30)").await?;
conn.execute("INSERT INTO users VALUES (2, 'Bob', 25)").await?;
conn.commit().await?;Sourcepub async fn stats(&self) -> PoolStats
pub async fn stats(&self) -> PoolStats
Get pool statistics
§Examples
let stats = client.stats().await;
println!("Active connections: {}", stats.active_connections);Sourcepub fn auth_manager(&self) -> &Arc<AuthManager>
pub fn auth_manager(&self) -> &Arc<AuthManager>
Get the authentication manager for user management
§Examples
let auth = client.auth_manager();
auth.create_user("alice", "password123", vec![Permission::Select]).await?;Sourcepub async fn fork(&self) -> Result<Self>
pub async fn fork(&self) -> Result<Self>
Create a lightweight, isolated fork of the current database.
This uses Copy-On-Write (COW) mechanics, so it is an O(1) operation regardless of database size. It is perfect for test isolation.
§Examples
let master = Client::connect_local("admin", "pass").await?;
master.execute("CREATE TABLE test (id INT)").await?;
// Create two independent forks
let test1 = master.fork().await?;
let test2 = master.fork().await?;
test1.execute("INSERT INTO test VALUES (1)").await?;
test2.execute("INSERT INTO test VALUES (2)").await?;
// Master is untouched
let count = master.query("SELECT * FROM test").await?.row_count();
assert_eq!(count, 0);