Expand description
§Premix ORM
“Write Rust, Run Optimized SQL.”
Premix is a zero-overhead, type-safe ORM for Rust. It generates SQL at
compile time with macros and executes via sqlx.
§Key Features
- Auto-sync schema from models.
- Compile-time SQL generation (no runtime reflection).
- Application-level joins with batched
WHERE INqueries. - SQLite, Postgres, and MySQL support via
sqlx.
§Quick Start
use premix_orm::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Model, Debug, Serialize, Deserialize)]
struct User {
id: i32,
name: String,
}
let pool = Premix::smart_sqlite_pool("sqlite::memory:").await?;
premix_orm::Premix::sync::<premix_orm::sqlx::Sqlite, User>(&pool).await?;
let mut user = User { id: 0, name: "Alice".to_string() };
user.save(&pool).await?;§Relations and Eager Loading
use premix_orm::prelude::*;
#[derive(Model)]
struct User {
id: i32,
name: String,
#[has_many(Post, eager)]
#[premix(ignore)]
posts: Option<Vec<Post>>,
}
#[derive(Model)]
#[belongs_to(User)]
struct Post {
id: i32,
user_id: i32,
title: String,
}
let _users = User::find_in_pool(&pool).include(User::posts).all().await?;§Bulk Operations
use premix_orm::prelude::*;
use serde_json::json;
#[derive(Model)]
struct User {
id: i32,
status: String,
}
let _updated = User::find_in_pool(&pool)
.filter_eq("status", "inactive")
.update(json!({ "status": "active" }))
.await?;§Installation
[dependencies]
premix-orm = "1.0.9-alpha"§Book
A longer-form guide lives in orm-book/ at the repository root. It covers
models, queries, relations, migrations, transactions, and limitations.
Re-exports§
Modules§
- chrono
- Chrono: Date and Time for Rust
- dialect
- SQL dialect abstractions for multi-database support.
- error
- Premix error types and helpers.
- executor
- Database executor abstraction for connection pools and transactions.
- integrations
- Integration with common web frameworks (Axum, Actix, etc.).
- metrics
metrics - Metrics and monitoring.
- migrator
- Database migration engine.
- model
- Core traits and types for database models.
- prelude
- The Premix prelude, re-exporting commonly used traits and types.
- query
- Type-safe SQL query builder.
- schema
- Database schema introspection and diffing utilities.
- sql_
cache - Cache helpers for SQL snippets/placeholders.
- sqlx
- The async SQL toolkit for Rust, built with ❤️ by the LaunchBadge team.
- tracing
- A scoped, structured logging and diagnostics system.
Macros§
- premix_
query - Compile-time query macro for true Zero-Overhead SQL generation.
- schema_
models - Helper macro to collect schema metadata from multiple models.
Structs§
- FastRow
- Wrapper for fast positional row decoding.
- Migration
- A single database migration.
- Migrator
- A migration manager for applying and rolling back migrations.
- Premix
- Main entry point for the Premix ORM helpers.
- Query
Builder - A type-safe SQL query builder.
- Relation
- Typed relation handle for eager loading.
- Validation
Error - An error that occurred during model validation.
Enums§
- Executor
- A unified database executor that can wrap either a connection pool or a single connection.
- Premix
Error - Premix-specific error type with actionable variants.
- Update
Result - The result of an update operation, particularly relevant for optimistic locking.
Traits§
- Into
Executor - A trait for types that can be converted into an
Executor. - Model
- The core trait for database models.
- Model
Hooks - Hooks that can be implemented to run logic before or after database operations.
- Model
Schema - A trait for models that can provide their own schema metadata.
- Model
Validation - A trait for validating model data before it is saved to the database.
- SqlDialect
- A trait that encapsulates all the requirements for a database to work with Premix.
Functions§
- build_
placeholders - Helper to build a comma-separated list of placeholders for a given database.
- cached_
placeholders - Returns a cached placeholder list for
(DB, count)combinations. - cached_
placeholders_ from - Returns a cached placeholder list for
(DB, start, count)combinations. - map_
sqlx_ error - Convert sqlx errors to actionable Premix errors when possible.
Type Aliases§
- Premix
Result - Result alias for Premix operations.