Skip to content

Getting started

  1. Add the crates. Mabat runs on SQLx 0.9 with the Tokio runtime. Choose the databases with features; postgres is 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"] }
  2. 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,
    }
  3. 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?;
  4. See the queries Mabat runs: one for the tasks, one for the assignees, one for the subtasks.

    println!("{}", mabat::plan::<TaskView>()?.explain());