typed_sqlx_client
A type-safe, extensible Rust library for managing multiple SQL databases and tables with sqlx. Build robust database applications with automatic CRUD operations, flexible queries, and compile-time safety.
✨ Key Features
- 🚀 Automatic CRUD:
#[derive(CrudOpsRef)]generates full CRUD operations - 🛡️ Type Safety: Compile-time prevention of database/table mix-ups
- 🗄️ Multi-Database: MySQL, PostgreSQL, and SQLite with a unified API
- 🔍 Flexible Queries: Type-safe & JSON SELECTs with safe parameter binding
🚀 Quick Start
[]
= "0.2.5"
= { = "0.8", = ["postgres", "runtime-tokio"] }
use ;
use ;
;
async
🏷️ Attributes
| Attribute | Level | Description |
|---|---|---|
#[crud(table = "...")] |
struct | Table name (defaults to struct name) |
#[crud(db = "...")] |
struct | mysql / postgres / sqlite |
#[crud(primary_key)] |
field | Mark primary key (defaults to first field) |
#[crud(rename = "...")] |
field | Map field to a column name |
#[crud(encode_json)] |
field | Auto Json() wrap on insert/update |
🔍 Query API
SelectOnlyQuery is implemented directly on SqlTable. Only SELECT statements
are allowed; prefer .bind() over format! interpolation for safe parameter binding.
| Method | Input | Output |
|---|---|---|
execute_select_only(&str) |
raw SQL | Vec<serde_json::Value> |
execute_select_as_only::<T>(&str) |
raw SQL | Vec<T> |
execute_select_query(Query) |
sqlx::query(...).bind(...) |
Vec<serde_json::Value> |
execute_select_as_query(QueryAs) |
sqlx::query_as::<DB, T>(...).bind(...) |
Vec<T> |
🗄️ Multi-Database
;
;
let main = ;
let analytics = ;
let users = main.; // ✅
let events = analytics.; // ✅
// let wrong = main.get_table::<Event>(); // ❌ compile error
📖 Documentation
🤝 Contributing
Contributions are welcome! Feel free to open an issue or submit a Pull Request.
📄 License
MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.