Tauri banner

Prerequisites

Before you start, make sure you:

1

Add the following crates to the Tauri project's dependencies

src-tauri/Cargo.toml
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11.22", features = ["json", "blocking"] }
dotenvy = "0.15.7"
tokio = { version = "1", features = ["full"] }
libsql = { git = "https://github.com/tursodatabase/libsql" }
tracing-subscriber = "0.3"
tracing = "0.1.40"

[patch.crates-io]
sqlite3-parser = { git = "https://github.com/LucioFranco/lemon-rs" }
2

Fetch Rust dependencies inside the `src-tauri` directory.

cargo fetch
3

Configure database credentials

Get the database URL.

turso db show --url <database-name>

Get the database authentication token.

turso db tokens create <database-name>

Assign acquired credentials to the environment variables inside src-tauri/.env.

src-tauri/.env
TURSO_SYNC_URL="..."
TURSO_AUTH_TOKEN="..."
DB_PATH=./my-data.db # embedded replica
4

Execute SQL

src-tauri/src/main.rs
use dotenvy::dotenv;
use libsql::{params, Database};
use serde::{Deserialize, Serialize};
use std::env;

use tracing::info;

#[derive(Serialize, Debug)]
struct Error {
    msg: String,
}

type Result<T> = std::result::Result<T, Error>;

impl<T> From<T> for Error
where
    T: std::error::Error,
{
    fn from(value: T) -> Self {
        Self {
            msg: value.to_string(),
        }
    }
}

#[derive(Deserialize, Serialize, Debug)]
pub struct Item {
    id: String,
    foo: String,
    bar: String,
}

#[tauri::command]
async fn get_all_items() -> Result<Vec<Item>> {
    dotenv().expect(".env file not found");

    let db_path = env::var("DB_PATH").unwrap();
    let sync_url = env::var("TURSO_SYNC_URL").unwrap();
    let auth_token = env::var("TURSO_AUTH_TOKEN").unwrap();

    let db = Database::open_with_remote_sync(db_path, sync_url, auth_token).await?;

    let conn = db.connect()?;

    let mut results = conn
        .query("SELECT * FROM table_name", ())
        .await?;

    let mut items: Vec<Item> = Vec::new();
    while let Some(row) = results.next()? {
        let note: Item = Item {
            id: row.get(0)?,
            foo: row.get(1)?,
            bar: row.get(2)?,
        };
        items.push(item);
    }

    Ok(items)
}

fn main() {
    tracing_subscriber::fmt::init();

    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            get_all_notes,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Examples