grorm
GRoutines + ORM — A goroutine-native async ORM for Rust with multi-database support.
Features
- Multi-database: PostgreSQL, MySQL, SQLite
- Goroutine-native: Built on gorust, no tokio required
- Chainable API:
where_eq().limit().offset().order().find() - Transactions:
Transaction::begin()with auto-rollback on drop - Auto table creation:
create_table()generates DDL from model definitions - Index & constraints:
#[index],#[unique],#[unique_index = "name"] - JOIN support:
left_join(),inner_join(),right_join() - IN queries:
where_in("name", vec![...]) - Connection pooling: gorust channel-based connection pool
- Derive macros:
#[derive(DeriveModel)]auto-generatesModeltrait
Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
= "1.5"
SQLite Example
use ;
use DeriveModel;
use runtime;
PostgreSQL Example
use ;
use DeriveModel;
use runtime;
MySQL Example
use ;
use DeriveModel;
use runtime;
Model Definition
Use #[derive(DeriveModel)] to define your model:
use DeriveModel;
// Override table name (default: snake_case + "s")
// Override primary key (default: "id")
Available Attributes
| Attribute | Scope | Description |
|---|---|---|
#[table = "name"] |
struct | Override table name |
#[primary_key = "col"] |
struct | Override primary key column |
#[index] |
field | Create a regular index |
#[unique] |
field | Create a unique constraint |
#[unique_index = "name"] |
field | Group into composite unique index |
CRUD Operations
Create Table
let mut qb = new;
qb.create_table?;
Insert
let user = User ;
let id = qb.insert?; // Returns Option<i64> (auto-generated id)
Query
// Find all
let all = qb.find_all?;
// Find by id
let user = qb.find_by_id?;
// Find one with conditions
let user = qb.where_eq.find_one?;
// Find with conditions
let users = qb.where_eq.find?;
// Find with column name
let users = qb.find_where?;
// Find with model (non-zero fields become conditions)
let filter = User ;
let users = qb.where_model.find?;
// IN query
let users = qb.where_in.find?;
// Pagination
let users = qb.order.limit.offset.find?;
// Count
let total = qb.count?;
Update
// Update single column
let rows = qb.where_eq
.update_one?;
// Update from model (non-zero/non-empty fields)
let update = User ;
let rows = qb.where_eq
.update_model?;
Delete
// Delete with conditions
let rows = qb.where_eq.delete?;
// Delete all
let rows = qb.delete_all?;
Transactions
let mut tx = begin?;
tx.insert?;
tx.where_eq.update_one?;
tx.commit?;
// If tx goes out of scope without commit, it auto-rolls back
All QueryBuilder methods are available on Transaction:
insert,find,find_all,find_one,find_by_id,find_wherewhere_eq,where_in,where_modelupdate_one,update_modeldelete,delete_allcount,limit,offset,order
JOIN Support
let mut qb = new;
qb.left_join
.inner_join
.right_join;
let results = qb.find?;
Connection Pool
// Create pool
let config = sqlite;
let pool = new;
// Get connection (blocks if pool exhausted)
let mut conn = pool.get?;
// Use driver directly
let mut qb = new;
Error Handling
All public APIs return Result<T, grorm::Error>. The Error enum covers:
| Variant | Description |
|---|---|
Connection |
Auth, network errors |
Query |
SQL syntax, constraint violations |
Execute |
Write operation errors |
Protocol |
Wire format, parsing errors |
Model |
Serialization/deserialization errors |
Pool |
Pool exhausted, closed |
Config |
Invalid DSN, configuration |
Io |
Wrapped I/O errors |
NotFound |
Entity not found |
Transaction |
Begin, commit, rollback errors |
Project Structure
grorm/
├── Cargo.toml
├── README.md
├── src/
│ ├── lib.rs # Library root, re-exports
│ ├── error.rs # Unified error types
│ ├── driver/ # Database driver abstraction
│ │ ├── mod.rs # ConnectionConfig, DatabaseDriver trait
│ │ ├── postgres.rs # PostgreSQL driver
│ │ ├── mysql.rs # MySQL driver
│ │ └── sqlite.rs # SQLite driver
│ ├── protocol/ # Database wire protocols
│ │ ├── mod.rs
│ │ ├── pg.rs # PostgreSQL protocol
│ │ ├── myproto.rs # MySQL protocol
│ │ └── sqlite_proto.rs # SQLite protocol (mock)
│ ├── query/ # Low-level SQL builders
│ │ ├── mod.rs
│ │ ├── select.rs
│ │ ├── insert.rs
│ │ ├── update.rs
│ │ └── delete.rs
│ ├── types/ # Type mapping (Rust ↔ SQL)
│ │ ├── mod.rs
│ │ ├── value.rs # Value enum
│ │ ├── from_sql.rs # FromSql trait
│ │ └── to_sql.rs # ToSql trait
│ ├── orm/ # ORM core
│ │ ├── mod.rs
│ │ ├── model.rs # Model trait, ColumnInfo
│ │ ├── query.rs # QueryBuilder (chainable API)
│ │ └── transaction.rs # Transaction support
│ └── pool/ # Connection pool (gorust channels)
│ └── mod.rs
├── grorm-macros/ # Procedural macros
│ ├── Cargo.toml
│ └── src/
│ └── lib.rs # #[derive(DeriveModel)]
└── examples/
├── sqlite_demo.rs
├── postgres_demo.rs
└── mysql_demo.rs
License
MIT