Crate deadpool_libsql

Source
Expand description

§Deadpool for libsql Latest Version Unsafe forbidden Rust 1.81+

Deadpool is a dead simple async pool for connections and objects of any type.

This crate implements a deadpool manager for libsql.

§Features

FeatureDescriptionExtra dependenciesDefault
rt_tokio_1Enable support for tokio cratedeadpool/rt_tokio_1yes
rt_async-std_1Enable support for async-std cratedeadpool/rt_async-std_1no
serdeEnable support for serde cratedeadpool/serde, serde/deriveno

All of the features of libsql are also re-exported. For example, the feature core does enable the feature core from the libsql crate.

§Example

use std::sync::Arc;

use deadpool_libsql::{Manager, Pool};
use deadpool_libsql::libsql::{Builder, params};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let db = deadpool_libsql::libsql::Builder::new_local("libsql.db")
        .build()
        .await?;

    let manager = Manager::from_libsql_database(db);
    let pool = Pool::builder(manager).build()?;

    let conn = pool.get().await?;
    let mut rows = conn.query("SELECT 1", params![]).await?;
    let row = rows.next().await?.unwrap();
    let result: i64 = row.get(0)?;

    Ok(())
}

§Example with config and dotenvy crate

# .env
LIBSQL__DATABASE=Local
LIBSQL__PATH=deadpool.db
use deadpool_libsql::{libsql::params, Runtime};
use dotenvy::dotenv;

#[derive(Debug, serde::Deserialize)]
struct Config {
    libsql: deadpool_libsql::Config,
}

impl Config {
    pub fn from_env() -> Result<Self, config::ConfigError> {
        config::Config::builder()
            .add_source(config::Environment::default().separator("__"))
            .build()?
            .try_deserialize()
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenv().ok();
    let cfg = Config::from_env().expect("Invalid config");
    let pool = cfg.libsql.create_pool(Some(Runtime::Tokio1)).await?;
    for i in 1..10i64 {
        let conn = pool.get().await?;
        let mut rows = conn.query("SELECT 1 + $1", params![i]).await?;
        let row = rows.next().await?.unwrap();
        let value: i64 = row.get(0)?;
        assert_eq!(value, i + 1);
    }
    Ok(())
}

§License

Licensed under either of

at your option.

Re-exports§

pub use config::Config;
pub use libsql;

Modules§

config
This module contains all the configuration structures

Structs§

Manager
Manager for creating and recycling libsql::Connection.
Metrics
Statistics regarding an object returned by the pool
PoolConfig
Pool configuration.
Status
The current pool status.
Timeouts
Timeouts when getting Objects from a Pool.

Enums§

ConnectionError
This error is returned when the connection fails
Runtime
Enumeration for picking a runtime implementation.

Type Aliases§

BuildError
Type alias for using deadpool::managed::BuildError with libsql.
Connection
Type alias for [‘Object’]
CreatePoolError
Type alias for using deadpool::managed::CreatePoolError with libsql.
Hook
Type alias for using deadpool::managed::Hook with libsql.
HookError
Type alias for using deadpool::managed::HookError with libsql.
Object
Type alias for using deadpool::managed::Object with libsql.
Pool
Type alias for using deadpool::managed::Pool with libsql.
PoolBuilder
Type alias for using deadpool::managed::PoolBuilder with libsql.
PoolError
Type alias for using deadpool::managed::PoolError with libsql.