Ormlite: An ORM in Rust for developers that love SQL
I've been working on ormlite for around a year, and it's now reaching the point where it's a massive boost in productivity for my Rust application server development. It's built on the amazing foundation of sqlx.
You might be interested for several reasons:
\- It now auto-generates migrations from Rust structs. (Currently auto generation is only for Postgres) To my knowledge, it is the only Rust ORM with this capability.
\- The join API, while in alpha, has far fewer moving pieces than any other Rust ORM. It only relies on the table structs themselves, and does not rely on relation traits (SeaORM) or modules (Diesel).
\- There's little to no query builder syntax to learn. The query builder basically joins together &str fragments of raw SQL. It strikes the right level of abstraction between composability, and having near-zero learning curve for anyone who already knows SQL.
Here's it in action:
use ormlite::model::*;
#[derive(Model, Debug)]
pub struct Person {
pub id: i32,
pub name: String,
pub age: i32,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// Start by making a database connection.
let mut conn = ormlite::SqliteConnection::connect(":memory:").await.unwrap();
/// You can insert the model directly.
let mut john = Person {
id: 1,
name: "John".to_string(),
age: 99,
}.insert(&mut conn).await?;
println!("{:?}", john);
/// After modifying the object, you can update all its fields.
john.age += 1;
john.update_all_fields(&mut conn).await?;
/// Query builder syntax closely follows SQL syntax, translated into chained function calls.
let people = Person::select()
.where_("age > ?").bind(50)
.fetch_all(&mut conn).await?;
println!("{:?}", people);
}
Check out the quickstart on Github to get started using it!