| Crates.io | sql-middleware |
| lib.rs | sql-middleware |
| version | 0.2.0 |
| created_at | 2025-03-14 13:32:25.4891+00 |
| updated_at | 2025-09-25 01:33:34.86183+00 |
| description | Lightweight async wrappers for tokio-postgres, rusqlite, libsql, turso, and tiberius. |
| homepage | https://github.com/derekfrye/sql-middleware |
| repository | https://github.com/derekfrye/sql-middleware |
| max_upload_size | |
| id | 1592211 |
| size | 427,746 |
Sql-middleware is a lightweight async wrapper for tokio-postgres, rusqlite, libsql, experimental turso, and tiberius (SQL Server), with deadpool connection pooling (except Turso, which doesn't have deadpool backend yet), and an async api. A slim alternative to SQLx; fewer features, but striving toward a consistent api.
Motivated from trying SQLx, not liking some issue others already noted, and wanting an alternative.
deadpoolBy default, postgres and sqlite database backends are enabled. You can selectively enable only the backends you need:
# Only include SQLite and LibSQL support
sql-middleware = { version = "0", features = ["sqlite", "libsql"] }
Available features:
sqlite: Enables SQLite supportpostgres: Enables PostgreSQL supportmssql: Enables SQL Server supportlibsql: Enables LibSQL support (local or remote)turso: Enables Turso (in-process, SQLite-compatible). Experimental. No deadpool support (yet).default: Enables common backends (sqlite, postgres). Enable others as needed.test-utils: Enables test utilities for internal testingMore examples available in the tests dir, and this is in-use with a tiny little website app, rusty-golf.
You can use the prelude to import everything you need, or import item by item.
use sql_middleware::prelude::*;
Similar api regardless of db backend.
| PostgreSQL | SQLite / LibSQL / Turso |
|---|---|
|
|
Note: The SQLite example applies to SQLite, LibSQL, and Turso. Swap the constructor as needed: new_sqlite(path), new_libsql(path), or new_turso(path). For Turso, there’s no deadpool pooling; get_connection creates a fresh connection.
Same api regardless of db backend.
// simple api for batch queries
let ddl_query =
include_str!("/path/to/test1.sql");
conn.execute_batch(&ddl_query).await?;
Consistent API using QueryAndParams. Only the placeholder syntax differs.
| PostgreSQL | SQLite / LibSQL / Turso |
|---|---|
|
|
Note: For LibSQL, construct with ConfigAndPool::new_libsql(path). For Turso, use ConfigAndPool::new_turso(path); there is no deadpool pooling for Turso — get_connection creates a new connection.
You can issue no-parameter queries directly, the same for PostgreSQL, SQLite, LibSQL, and Turso:
// Either build a QueryAndParams
let query = QueryAndParams::new_without_params("SELECT * FROM users");
let results = conn.execute_select(&query.query, &[]).await?;
// Or pass the SQL string directly
let results2 = conn.execute_select("SELECT * FROM users", &[]).await?;
Here, the APIs differ a bit, because the underlying libraries are different. It doesn't appear easy to make these consistent without hiding underlying library capabilities. This is the most similar way to do queries w this middleware if you need custom app logic between transaction() and commit().
See further examples in the tests directory:
| PostgreSQL / LibSQL / Turso | SQLite |
|---|---|
|
|
The AsyncDatabaseExecutor trait provides a consistent interface for database operations:
// This works for PostgreSQL, SQLite, LibSQL, and Turso connections
async fn insert_user<T: AsyncDatabaseExecutor>(
conn: &mut T,
user_id: i32,
name: &str
) -> Result<(), SqlMiddlewareDbError> {
let query = QueryAndParams::new(
// Use appropriate parameter syntax for your DB
// PostgreSQL: "VALUES ($1, $2)"
// SQLite/LibSQL/Turso: "VALUES (?, ?)" or "VALUES (?1, ?2)"
"INSERT INTO users (id, name) VALUES ($1, $2)",
vec![
RowValues::Int(user_id),
RowValues::Text(name.to_string()),
]
);
// Execute query through the trait. Placeholder style in `query.query`
// must match the active backend.
conn.execute_dml(&query.query, &query.params).await?;
Ok(())
}
#[allow(clippy::unused_async)] and our async API design philosophy.cargo buildcargo build --features tursocargo testcargo test --features tursocargo test --features libsql