From 8ca1b92aa65b46eaeee7017e789bed0af59ed402 Mon Sep 17 00:00:00 2001 From: Michael de Silva Date: Sat, 9 Nov 2024 13:51:08 +0530 Subject: [PATCH 1/2] Require CLI example for example to use Sqlite DB driver to persist to local file --- .gitignore | 1 + examples/hello-toasty/src/main.rs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 4fffb2f..4ff5852 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /Cargo.lock +/toasty.db3 diff --git a/examples/hello-toasty/src/main.rs b/examples/hello-toasty/src/main.rs index b30f710..e69aa56 100644 --- a/examples/hello-toasty/src/main.rs +++ b/examples/hello-toasty/src/main.rs @@ -1,5 +1,5 @@ mod db; -use std::path::PathBuf; +use std::{env, path::PathBuf}; use db::{Todo, User}; @@ -10,16 +10,25 @@ fn assert_sync_send(_: T) {} #[tokio::main] async fn main() { - let schema_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("schema.toasty"); + let args: Vec = env::args().collect(); + let flag = "--persist-to-file"; + + let driver = if args.len() > 1 && args[1] == flag { + let filename = "toasty.db3"; + let file_path = format!("./{}", filename); + let file = PathBuf::from(file_path.as_str()); + Sqlite::open(file).unwrap() + } else { + // Use the in-memory sqlite driver + Sqlite::in_memory() + }; + let schema_file = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("schema.toasty"); let schema = toasty::schema::from_file(schema_file).unwrap(); // NOTE enable this to see the enstire structure in STDOUT // println!("{schema:#?}"); - // Use the in-memory sqlite driver - let driver = Sqlite::in_memory(); - let db = Db::new(schema, driver).await; // For now, reset!s db.reset_db().await.unwrap(); From e4c98ee3f3cf58a681e6ed3f6c8a025149736ee6 Mon Sep 17 00:00:00 2001 From: Michael de Silva Date: Sat, 9 Nov 2024 13:54:54 +0530 Subject: [PATCH 2/2] Improve comments for example --- examples/hello-toasty/src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/hello-toasty/src/main.rs b/examples/hello-toasty/src/main.rs index e69aa56..5f2dcd4 100644 --- a/examples/hello-toasty/src/main.rs +++ b/examples/hello-toasty/src/main.rs @@ -11,9 +11,11 @@ fn assert_sync_send(_: T) {} #[tokio::main] async fn main() { let args: Vec = env::args().collect(); - let flag = "--persist-to-file"; + // CLI flag required to persist DB to local file, instead of using the in-memory sqlite driver. + let flag = "--persist-to-file"; let driver = if args.len() > 1 && args[1] == flag { + // Persist to local file let filename = "toasty.db3"; let file_path = format!("./{}", filename); let file = PathBuf::from(file_path.as_str());