slintrust
slintrust is a lightweight, Rust-based ORM designed to simplify database operations using procedural macros. It eliminates the need for manually writing table schemas, migrations, or repetitive SQL commands.
Features
- Simple table definition using
#[slint] macro.
- Automatic schema generation and migrations.
- Supports basic CRUD operations: insert, query, get all, and raw SQL execution.
- Works asynchronously with
tokio and sqlx.
- Multi-table support.
Installation
Add this to your Cargo.toml:
[dependencies]
slintrust = "0.1.0"
Example usage
use slint_derive::slint;
use slintrust::*;
use serde::{Deserialize, Serialize};
use serde_json::json;
#[slint(table_name = "userx_table")]
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
pub id: String,
pub name: String,
pub email: String,
}
#[slint(table_name = "postsx_table")]
#[derive(Debug, Serialize, Deserialize)]
pub struct Postsx {
pub id: String,
pub name: String,
pub email: String,
pub user_d: String,
}
#[tokio::main]
async fn main() -> sqlx::Result<()> {
let mut orm = OrmStruct::new(
"postgres://postgres@localhost:5432/postgres".into(),
vec![
User::slint_schema(),
Postsx::slint_schema(),
],
);
orm.connect().await?;
orm.migrate().await?;
let user = User {
id: "".into(),
name: "Ada".into(),
email: "ada@mail.com".into(),
};
orm.insert("userx_table", &user).await?;
let users: Vec<User> = orm
.query("userx_table")
.like("name", "Ad")
.limit(5)
.fetch_all()
.await?;
println!("Users via old query API: {:?}", users.len());
let ada: Option<User> =
orm.first("userx_table", "email", "ada@mail.com").await?;
println!("First user via old API: {:?}", ada);
let user_table = Table::<User>::new(orm.clone(), "userx_table", "email");
let post_table = Table::<Postsx>::new(orm.clone(), "postsx_table", "email");
user_table.insert(&User {
id: "".into(),
name: "Grace".into(),
email: "grace@mail.com".into(),
}).await?;
let record = user_table
.get(json!({ "email": "ada@mail.com" }))
.await?;
if let Some(user) = &record {
println!("Fetched via Table.get(): {:?}", user.value);
}
let queried = user_table
.query()
.where_clause("name", "LIKE", "%Ada%")
.order_by("name", "ASC")
.limit(10)
.get()
.await?;
println!("Advanced query results: {}", queried.len());
let first_user = user_table
.query()
.where_clause("email", "=", "ada@mail.com")
.first()
.await?;
if let Some(userx) = first_user {
println!("First via new Query API: {:?}", userx.value);
let updated = userx.update(json!({"name": "joy"})).await?;
println!("Updated user: {:?}", updated);
userx.delete().await?;
println!("User deleted");
}
Ok(())
}
slintrust