ormkit
A compile-time safe async ORM for PostgreSQL powered by SQLx.
Core ORM Features
- Type-Safe Query DSL: Compile-time checked queries with fluent API
- Entity Derive:
#[derive(ormkit::Entity)]for automatic trait implementation - Repository Pattern: Generic CRUD operations with type-safe IDs
- Query Builder: Dynamic filters and ordering with type safety
- Pagination: Offset and cursor-based pagination
- Transactions: Automatic commit/rollback with nested transaction support
Optional Production Features
Enable with feature flags (see Installation):
- Migrations (
migrations): Version-controlled schema migrations with CLI - Schema Validation (
validate): Runtime entity-to-database validation - Performance (
performance): Query/entity caching, parallel execution, smart pooling - Relationships: BelongsTo and HasMany with N+1 prevention (basic implementation)
Installation
[]
= "0.2"
= { = "0.8", = ["runtime-tokio-rustls", "postgres", "chrono", "uuid", "json"] }
Feature Flags
# Core ORM only (minimal dependencies)
= { = "0.2", = ["orm"] }
# Core + migrations (recommended for most projects)
= { = "0.2", = ["orm", "migrations"] }
# Core + production features
= { = "0.2", = ["full"] }
# All features (default)
= { = "0.2" }
Quick Start
use ;
use ActiveValue;
use PgPool;
use Uuid;
// Define an Entity using the derive macro
// Repository operations (CRUD)
async
Core Features
Type-Safe Query Builder (Recommended)
use Entity;
// Compile-time type-safe queries
let users: = query
.filter
.filter
.order_by
.limit
.fetch_all
.await?;
Dynamic Query Builder (for admin/filters)
use ;
// Runtime query construction (for admin panels, dynamic filters)
let mut query = query;
if let Some = email_filter
if let Some = min_age
let users = query.fetch_all.await?;
Pagination
use PaginationRequest;
let repo = new;
let request = new; // page 1, 25 per page
let page = repo.paginate.await?;
println!;
for user in page.items
Batch Operations
use ;
let new_users = vec!;
let options = new.batch_size;
let result = insert_many.await?;
println!;
Transactions
use transaction;
let result = transaction.await?;
Advanced Features
Relationships (Prevents N+1 Queries)
use RelationBuilder;
// Load related entities in a single query
let posts_with_users = for_entities
.
.await?;
for loaded in posts_with_users
Project Status
v0.2.1 - Stable core ORM with production features
Production Ready
- Entity derive macros with compile-time type safety
- Type-safe query DSL with fluent API
- Repository CRUD operations
- Query builder with dynamic filters
- Pagination (offset and cursor-based)
- Transaction management with nested transactions
- Migration engine with CLI
- Schema validation
Performance Characteristics
- Matches SQLx for single queries
- Can outperform naive SQLx usage in specific scenarios:
- Cached lookups (up to 2000x speedup on warm cache)
- Bulk operations with parallel execution
- N+1 query prevention
- Raw SQLx remains fastest for single, one-off queries
In Development
- Relationship ergonomics (BelongsTo/HasMany work, API needs polish)
- One-to-one and many-to-many relationships
- Typed ActiveModel derive macro integration
Platform Support
- PostgreSQL: 12, 13, 14, 15, 16
- Rust: 1.70+ (MSRV)
- Tokio: 1.x
Documentation
Getting Started (5-10 min read)
- Quick Start - Basic usage example
- Core ORM Guide - Essential features and patterns
- API Reference - Complete API documentation
In-Depth Guides
- Relationships Guide - BelongsTo, HasMany, N+1 prevention
- ActiveModel Guide - Typed vs HashMap change tracking
- Type-Safe Queries - Query DSL
- Query Builder - Dynamic queries
Production Topics
- Production Guide - Migrations, validation, performance features
- Performance Optimizations - Caching, batching, benchmarks
- Error Handling - Error types, retry logic, categorization
Reference
- Project Status - Feature status and platform support
- License - MIT OR Apache-2.0
License
MIT OR Apache-2.0
Ormkit — A compile-time safe async ORM for PostgreSQL powered by SQLx.