Rust quickstart

This guide creates an in-memory Lix repository, writes a file, reads its history, and uses SQL to undo the latest change.

Install

cargo add lix
cargo add tokio --features macros,rt-multi-thread

Write and update a file

#[tokio::main]
async fn main() -> Result<(), lix::LixError> {
    let lix = lix::open_lix().await?;

    lix.execute("INSERT INTO lix_file (path, content) VALUES ($1, $2)")
        .bind("/hello.txt")
        .bind(lix::Value::Blob(b"Hello".as_slice().into()))
        .await?;
    lix.execute("UPDATE lix_file SET content = $1 WHERE path = $2")
        .bind(lix::Value::Blob(b"Hello from Lix".as_slice().into()))
        .bind("/hello.txt")
        .await?;

    let history = lix
        .query(
            "SELECT from_path, to_path, diff_type, lixcol_position \
             FROM lix_history('lix_file') \
             WHERE from_path = $1 OR to_path = $1 \
             ORDER BY lixcol_position",
        )
        .bind("/hello.txt")
        .all()
        .await?;

    println!("{} versions", history.rows().len());

    let undone = lix.execute("SELECT commit_id FROM lix_undo()").await?;
    println!("undo receipt {:?}", undone.rows().first());
    lix.close().await?;
    Ok(())
}

Every tracked write becomes a commit automatically. You never run a commit command. Position 0 is the head commit. Higher positions walk back through the first-parent chain.

query() reads and never writes; its terminals are all(), one(), optional() and stream(). execute() runs any single statement. To run several statements atomically, use lix.begin_transaction(); to run one statement with many parameter sets, use lix.execute_many(). Do not concatenate SQL into one script string.

The repository is in memory and disappears when the process ends. To keep it on disk, use lix-storage-rocksdb or lix-storage-filesystem. See Storage.

Remote and synchronized access

Supply only a server for remote execution. Supply storage and a server for a partial replica that syncs in the background. See Storage for the setup and Hosting for creating and deleting hosted repositories from Rust.

Next