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..5f2dcd4 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,27 @@ 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(); + + // 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()); + 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();