sqlx-turso-driver 0.0.1

An asynchronous SQLx driver for embedded Turso databases
//! Handcrafted temporary data only; takes no user database path or credentials.
use sqlx_turso_driver::{Turso, TursoConnectOptions, TursoPool, connect_pool, sqlx};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let directory = tempfile::Builder::new()
        .prefix("sqlx-turso-local-example-")
        .tempdir()?;
    let options = TursoConnectOptions::file(directory.path().join("本地 sample.db"))?;
    let pool: TursoPool = connect_pool(options.clone(), 1).await?;
    sqlx::query("CREATE TABLE samples(id INTEGER PRIMARY KEY, label TEXT NOT NULL)")
        .execute(&pool)
        .await?;
    let inserted = sqlx::query("INSERT INTO samples VALUES (?1, ?2)")
        .bind(1_i64)
        .bind("handcrafted seed")
        .execute(&pool)
        .await?;
    assert_eq!(inserted.rows_affected(), 1);

    let mut tx = pool.begin().await?;
    sqlx::query("INSERT INTO samples VALUES (?1, ?2)")
        .bind(2_i64)
        .bind("committed 世界")
        .execute(&mut *tx)
        .await?;
    tx.commit().await?;

    let mut discarded = pool.begin().await?;
    sqlx::query("INSERT INTO samples VALUES (?1, ?2)")
        .bind(3_i64)
        .bind("rolled back")
        .execute(&mut *discarded)
        .await?;
    discarded.rollback().await?;
    pool.close().await;

    let reopened = connect_pool(options, 1).await?;
    let rows = sqlx::query_as::<Turso, (i64, String)>("SELECT id, label FROM samples ORDER BY id")
        .fetch_all(&reopened)
        .await?;
    assert_eq!(
        rows,
        vec![
            (1, "handcrafted seed".to_owned()),
            (2, "committed 世界".to_owned()),
        ]
    );
    reopened.close().await;
    println!("local Turso: create/bind/commit/rollback/close/reopen verified (2 handcrafted rows)");
    // Only the owned temp directory and its database/WAL/lock sidecars are removed.
    directory.close()?;
    Ok(())
}