use dotenv::dotenv;
use libsql::de::from_row;
use libsql::Builder;
use tokio_stream::StreamExt;
#[derive(serde::Deserialize, Debug)]
struct Todo {
id: i32,
title: String,
completed: bool,
}
#[tokio::main]
async fn main() {
dotenv().ok();
let url = std::env::var("LIBSQL_URL").unwrap();
let token = std::env::var("LIBSQL_TOKEN").unwrap();
let db = Builder::new_remote_replica("local.db", url, token)
.build()
.await
.unwrap();
let conn = db.connect().unwrap();
conn.execute("DROP TABLE todos", ()).await.unwrap();
conn.execute(
"CREATE TABLE todos (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,completed BOOLEAN NOT NULL)",
(),
)
.await
.expect("create table");
conn.execute_batch(
"
INSERT INTO
todos (title, completed)
VALUES
('Buy groceries', 0);
INSERT INTO
todos (title, completed)
VALUES
('Clean the house', 0);
INSERT INTO
todos (title, completed)
VALUES
('Finish Rust project', 1);
INSERT INTO
todos (title, completed)
VALUES
('Read a book', 0);
",
)
.await
.expect("insert");
db.sync().await.unwrap();
let todos = conn.query("SELECT * FROM todos", ()).await.unwrap();
let stream = todos.into_stream();
let todos = stream
.map(|row| dbg!(from_row::<Todo>(&row.unwrap()).unwrap()))
.collect::<Vec<_>>()
.await;
assert_eq!(todos.len(), 4);
}
[dependencies]
dotenv = "*"
libsql = { path = "../libsql/libsql/" }
tokio-stream = "*"
tokio = { version = "1", features = ["full"] }
serde = "*"
Reproducer: