Skip to main content

Crate premix_orm

Crate premix_orm 

Source
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 IN queries.
  • 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§

pub use integrations::axum::*;axum
pub use integrations::actix::*;actix

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.).
metricsmetrics
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.
QueryBuilder
A type-safe SQL query builder.
Relation
Typed relation handle for eager loading.
ValidationError
An error that occurred during model validation.

Enums§

Executor
A unified database executor that can wrap either a connection pool or a single connection.
PremixError
Premix-specific error type with actionable variants.
UpdateResult
The result of an update operation, particularly relevant for optimistic locking.

Traits§

IntoExecutor
A trait for types that can be converted into an Executor.
Model
The core trait for database models.
ModelHooks
Hooks that can be implemented to run logic before or after database operations.
ModelSchema
A trait for models that can provide their own schema metadata.
ModelValidation
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§

PremixResult
Result alias for Premix operations.

Derive Macros§

Model