Getting started
-
Add the crates. Mabat runs on SQLx 0.9 with the Tokio runtime. Choose the databases with features;
postgresis the default.[dependencies]mabat = "0.1"sqlx = { version = "0.9", default-features = false, features = ["postgres", "runtime-tokio"] }tokio = { version = "1", features = ["macros", "rt-multi-thread"] }[dependencies]mabat = { version = "0.1", default-features = false, features = ["mysql"] }sqlx = { version = "0.9", default-features = false, features = ["mysql", "runtime-tokio"] }tokio = { version = "1", features = ["macros", "rt-multi-thread"] }[dependencies]mabat = { version = "0.1", default-features = false, features = ["sqlite"] }sqlx = { version = "0.9", default-features = false, features = ["sqlite", "runtime-tokio"] }tokio = { version = "1", features = ["macros", "rt-multi-thread"] } -
Declare views for the shape you want to read. Each view names its table; fields are columns unless an attribute makes them something else.
use mabat::View;#[derive(View, Debug)]#[view(table = "task")]struct TaskView {id: i64,name: String,description: Option<String>, // a nullable column#[view(to_one(fk = "assignee_id"))]assignee: Option<PersonView>, // a reference, loaded in one batched query#[view(child(fk = "parent_id", order_by = "position"))]children: Vec<SubtaskView>, // a collection, loaded in one batched query}#[derive(View, Debug)]#[view(table = "person")]struct PersonView {id: i64,#[view(column = "full_name")]name: String,}#[derive(View, Debug)]#[view(table = "task")]struct SubtaskView {id: i64,name: String,} -
Load on a connection, a transaction or a pool connection.
use mabat::filter::col;let mut conn = sqlx::PgConnection::connect(&url).await?;let task = mabat::load::<TaskView>().by_key(1_i64).one(&mut conn).await?;let open = mabat::load::<TaskView>().filter(col("status").eq("open")).order_by("name").limit(20).all(&mut conn).await?; -
See the queries Mabat runs: one for the tasks, one for the assignees, one for the subtasks.
println!("{}", mabat::plan::<TaskView>()?.explain());