| Crates.io | starberry_sql |
| lib.rs | starberry_sql |
| version | 0.6.4 |
| created_at | 2025-07-09 08:12:11.54+00 |
| updated_at | 2025-07-09 08:12:11.54+00 |
| description | SQL Middleware for Starberry |
| homepage | |
| repository | https://github.com/Redstone-D/starberry |
| max_upload_size | |
| id | 1744409 |
| size | 97,486 |
starberry_sql is an asynchronous SQL client and query builder designed for the Starberry ecosystem. It provides a simple, type-safe API to execute queries, map rows to structs, manage transactions, and handle connection pooling.
Encode traitSqlQueryFromRow traitQueryResult methodsSqlPoolSqlContextAdd starberry_sql to your Cargo.toml:
[dependencies]
starberry_sql = "0.6.0"
Then import in your code:
use starberry_sql::*;
use starberry_sql::{DbConnectionBuilder, DbError, SslMode};
#[tokio::main]
async fn main() -> Result<(), DbError> {
let mut conn = DbConnectionBuilder::new("127.0.0.1", 5432)
.ssl_mode(SslMode::Disable)
.database("postgres")
.username("postgres")
.password("secret")
.connect()
.await?;
Ok(())
}
let rows = SqlQuery::new("SELECT 1 AS a, 'foo' AS b")
.fetch_all(&mut conn)
.await?;
for row in rows {
println!("a = {}", row.get("a").unwrap());
}
let count = SqlQuery::new("INSERT INTO users (name, age) VALUES ($1, $2)")
.bind("Alice")
.bind(30)
.execute(&mut conn)
.await?;
println!("Inserted {} rows", count);
use std::collections::HashMap;
use starberry_sql::{FromRow, DbError};
#[derive(Debug)]
struct User { id: i32, name: String }
impl FromRow for User {
fn from_row(row: &HashMap<String, String>) -> Result<Self, DbError> {
Ok(User {
id: row.get("id").unwrap().parse().unwrap(),
name: row.get("name").unwrap().to_string(),
})
}
}
let users: Vec<User> = SqlQuery::new("SELECT id, name FROM users")
.fetch_all_as(&mut conn)
.await?;
let builder = DbConnectionBuilder::new("127.0.0.1", 5432)
.ssl_mode(SslMode::Disable)
.database("postgres")
.username("postgres")
.password("secret");
let pool = SqlPool::new(builder, 5);
let row = SqlQuery::new("SELECT COUNT(*) AS count")
.fetch_one_pool(&pool)
.await?;
conn.begin_transaction().await?;
// perform multiple statements
conn.commit_transaction().await?;
let total = conn.batch_execute(vec![
("INSERT INTO tx_test (id, name) VALUES ($1, $2)", vec!["1".to_string(), "One".to_string()]),
("INSERT INTO tx_test (id, name) VALUES ($1, $2)", vec!["2".to_string(), "Two".to_string()]),
])
.await?
.row_count();
let stmt = conn.prepare_statement("INSERT INTO tx_test (id, name) VALUES ($1, $2)")
.await?;
let result = conn.execute_prepared(&stmt, vec!["5".to_string(), "Five".to_string()])
.await?;
DbConnectionBuilder: Configure and establish database connections.SqlQuery: Build and execute queries with parameter binding.SqlPool: Manage a pool of connections for concurrent usage.SqlContext: Customize SQL execution context and settings.Encode trait: Convert Rust types into SQL-escaped literals.FromRow trait: Map a row (as HashMap<String, String>) into a struct.QueryResult: Handle query outcomes (Rows, Count, Empty, Error).DbError: Unified error type for connection and query operations.starberry_sql is released under the MIT License. See LICENSE for details.