Skip to main content

Client

Struct Client 

Source
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

Source

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?;
Source

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?;
Source

pub async fn connect_url(url: &str) -> Result<Self>

Connect using a connection string

Supports the following formats:

  • rustmemodb://username:password@host:port/database
  • postgres://username:password@host:port/database
  • postgresql://username:password@host:port/database
  • mysql://username:password@host:port/database
§Examples
let client = Client::connect_url(
    "postgres://admin:adminpass@localhost:5432/mydb"
).await?;
Source

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 clients
Source

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);
}
Source

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?;
Source

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?;
Source

pub async fn stats(&self) -> PoolStats

Get pool statistics

§Examples
let stats = client.stats().await;
println!("Active connections: {}", stats.active_connections);
Source

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?;
Source

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);

Trait Implementations§

Source§

impl DatabaseClient for Client

Source§

fn query<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<QueryResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a query that is expected to return rows (SELECT).
Source§

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<QueryResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute a query that modifies data (INSERT, UPDATE, DELETE, DDL).
Source§

fn ping<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Check if the connection is active

Auto Trait Implementations§

§

impl Freeze for Client

§

impl !RefUnwindSafe for Client

§

impl Send for Client

§

impl Sync for Client

§

impl Unpin for Client

§

impl !UnwindSafe for Client

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.