chopin-orm
High-fidelity engineering for the modern virtuoso.
chopin-orm is a high‑performance, type‑safe ORM built on top of chopin-pg. It provides derive-macro-driven model mapping, a fluent query DSL with type-safe column expressions, relationships, auto-migration, pagination, and ActiveModel partial updates.
Features
- Derive macro —
#[derive(Model)]generatesFromRow, column enum, and full CRUD methods - Type-safe column DSL —
UserColumn::name.eq("Alice")instead of raw strings - Relationships —
has_many/belongs_towith lazy loading and JOIN support - Auto-migration —
sync_schema()diffs and migrates table columns automatically - Pagination —
.paginate(page_size).page(n).fetch()returnsPage<M>with total counts - ActiveModel — partial updates tracking only changed fields
- Validation —
Validatetrait with default pass-through; implement custom rules - Upsert — INSERT ... ON CONFLICT UPDATE for idempotent writes
- Aggregations —
.count(),ColumnTrait::sum(),.max(),.min()with GROUP BY / HAVING - Mock executor —
MockExecutor+mock_row!for unit testing without a database - Logged executor —
LoggedExecutorwraps any executor for SQL tracing - Migration system —
MigrationManagerwithup/downfor production schema management
🛠️ Quick Start
use ;
use PgConfig;
🔗 Relationships
// Lazy loading
let posts = user.fetch_posts?; // has_many
let author = post.fetch_user_id?; // belongs_to
// JOIN queries
let users_with_posts = find
.
.all?;
📦 ActiveModel (Partial Updates)
use ActiveModel;
let user = find.filter.one?.unwrap;
let mut active = from_model;
active.set;
active.save?; // UPDATE users SET name = $1 WHERE id = $2
📄 Pagination
let page = find
.filter
.order_by
.paginate // 20 items per page
.page // page 1
.fetch?;
println!;
🧪 Testing with MockExecutor
use MockExecutor;
use Row;
let mut mock = new;
mock.push_result;
let users = find.all?;
assert_eq!;
assert_eq!;