-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libsql: Initial offline write support
This patch add initial support for offline writes. To open a local database that can sync to remote server, use the Builder::new_synced_database() API.
- Loading branch information
Showing
9 changed files
with
716 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Example of using a offline writes with libSQL. | ||
|
||
use libsql::{params, Builder}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
tracing_subscriber::fmt::init(); | ||
|
||
// The local database path where the data will be stored. | ||
let db_path = std::env::var("LIBSQL_DB_PATH") | ||
.map_err(|_| { | ||
eprintln!( | ||
"Please set the LIBSQL_DB_PATH environment variable to set to local database path." | ||
) | ||
}) | ||
.unwrap(); | ||
|
||
// The remote sync URL to use. | ||
let sync_url = std::env::var("LIBSQL_SYNC_URL") | ||
.map_err(|_| { | ||
eprintln!( | ||
"Please set the LIBSQL_SYNC_URL environment variable to set to remote sync URL." | ||
) | ||
}) | ||
.unwrap(); | ||
|
||
let namespace = std::env::var("LIBSQL_NAMESPACE").ok(); | ||
|
||
// The authentication token to use. | ||
let auth_token = std::env::var("LIBSQL_AUTH_TOKEN").unwrap_or("".to_string()); | ||
|
||
let db_builder = if let Some(ns) = namespace { | ||
Builder::new_synced_database(db_path, sync_url, auth_token).namespace(&ns) | ||
} else { | ||
Builder::new_synced_database(db_path, sync_url, auth_token) | ||
}; | ||
|
||
let db = match db_builder.build().await { | ||
Ok(db) => db, | ||
Err(error) => { | ||
eprintln!("Error connecting to remote sync server: {}", error); | ||
return; | ||
} | ||
}; | ||
|
||
let conn = db.connect().unwrap(); | ||
|
||
conn.execute( | ||
r#" | ||
CREATE TABLE IF NOT EXISTS guest_book_entries ( | ||
text TEXT | ||
)"#, | ||
(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let mut input = String::new(); | ||
println!("Please write your entry to the guestbook:"); | ||
match std::io::stdin().read_line(&mut input) { | ||
Ok(_) => { | ||
println!("You entered: {}", input); | ||
let params = params![input.as_str()]; | ||
conn.execute("INSERT INTO guest_book_entries (text) VALUES (?)", params) | ||
.await | ||
.unwrap(); | ||
} | ||
Err(error) => { | ||
eprintln!("Error reading input: {}", error); | ||
} | ||
} | ||
let mut results = conn | ||
.query("SELECT * FROM guest_book_entries", ()) | ||
.await | ||
.unwrap(); | ||
println!("Guest book entries:"); | ||
while let Some(row) = results.next().await.unwrap() { | ||
let text: String = row.get(0).unwrap(); | ||
println!(" {}", text); | ||
} | ||
|
||
print!("Syncing database to remote..."); | ||
db.sync().await.unwrap(); | ||
println!(" done"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -158,6 +158,7 @@ cfg_parser! { | |
|
||
mod rows; | ||
mod statement; | ||
mod sync; | ||
mod transaction; | ||
mod value; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.