[#2] Adjusted the database to use SQLX for async.

It was determined that Async database access would be prefereable incase
we decide to use a network database instead of sqlite.

Tests and examples need to be checked, but they were made to build.
This commit is contained in:
2025-08-29 18:11:17 -04:00
parent c9c03f9059
commit 0a16667b76
8 changed files with 1946 additions and 320 deletions

View File

@ -1,11 +1,11 @@
use std::path::{Path, PathBuf};
use chrono::{NaiveDate, NaiveDateTime};
use tavern::{Adventurer, Database, Tale, Tavern};
use chrono::NaiveDate;
use tavern::{Adventurer, Database, FrontMatter, Tale, Tavern};
#[cfg(feature = "publisher")]
fn generate_tavern() -> Tavern
{
let author: Adventurer =
@ -17,14 +17,21 @@ fn generate_tavern() -> Tavern
String::from("https://cybermages.tech/about/myrddin/pic"),
blurb: String::from("I love code!") };
let tale: Tale =
Tale { title: String::from("Test post"),
slug: String::from("test_post"),
author: author.handle.clone(),
summary: String::from("The Moon is made of cheese!"),
tags: vec![String::from("Space"), String::from("Cheese")],
publish_date: NaiveDate::from_ymd_opt(2025, 12, 25).unwrap().and_hms_opt(13, 10, 41).unwrap(),
content: PathBuf::from("posts/test_post.md") };
let fm: FrontMatter =
FrontMatter { title: String::from("Test post"),
slug: String::from("test_post"),
author: author.handle.clone(),
summary: String::from("The Moon is made of cheese!"),
tags: vec![String::from("Space"),
String::from("Cheese")],
publish_date:
NaiveDate::from_ymd_opt(2025, 12, 25).unwrap()
.and_hms_opt(13,
10, 41)
.unwrap() };
let tale: Tale = Tale { front_matter: fm,
content: PathBuf::from("posts/test_post.md") };
// Create a dummy posts directory and file for this example to work
@ -33,16 +40,21 @@ fn generate_tavern() -> Tavern
std::fs::create_dir("posts").unwrap();
}
std::fs::write("posts/the-rustacean.md",
"# Hello, Rust!\n\nThis is a **test** post.").unwrap();
"# Hello, Rust!\n\nThis is a **test** post.").unwrap();
Tavern { title: String::from("Runes & Ramblings"),
description: String::from("Join software engineer Jason Smith on his Rust programming journey. Explore program design, tech stacks, and more on this blog from CybeMages, LLC."),
description: String::from("Join software engineer Jason Smith \
on his Rust programming journey. \
Explore program design, tech \
stacks, and more on this blog from \
CybeMages, LLC."),
tales: vec![tale],
authors: vec![author] }
}
#[cfg(feature = "publisher")]
fn read_from_file<P>(config_file: P) -> Tavern
where P: AsRef<Path>
{
@ -55,37 +67,40 @@ fn read_from_file<P>(config_file: P) -> Tavern
}
fn create_database() -> Result<(), Box<dyn std::error::Error>>
#[cfg(feature = "publisher")]
async fn create_database() -> Result<(), Box<dyn std::error::Error>>
{
// This part would be the entry point of your CI/CD script
// It would load your data and then save it to the database
// Create a Tavern object
let tavern = read_from_file("Tavern.toml");
//let tavern = generate_tavern();
let _tavern = read_from_file("Tavern.toml");
// let tavern = generate_tavern();
// Open the database and save the Tavern content
let db = Database::open(Path::new("tavern.db"))?;
db.insert_tavern(&tavern.title, &tavern.description)?;
println!("Saved site settings: Title='{}', Description='{}'", tavern.title, tavern.description);
for author in &tavern.authors
{
db.insert_adventurer(author)?;
println!("Saved adventurer: {}", author.name);
}
for tale in &tavern.tales
{
db.insert_tale(tale)?;
println!("Saved tale: {}", tale.title);
}
let _db = Database::open("/home/myrddin/cybermages/blog/tavern.db").await?;
//db.insert_tavern(&tavern.title, &tavern.description)?;
//println!("Saved site settings: Title='{}', Description='{}'",
//tavern.title, tavern.description);
//for author in &tavern.authors
// {
// db.insert_adventurer(author)?;
// println!("Saved adventurer: {}", author.name);
// }
//
// for tale in &tavern.tales
// {
// db.insert_tale(tale)?;
// println!("Saved tale: {}", tale.title);
// }
Ok(())
}
pub fn main()
#[cfg(feature = "publisher")]
#[tokio::main]
pub async fn main()
{
match std::env::set_current_dir("/home/myrddin/cybermages/blog/")
{
@ -99,9 +114,18 @@ pub fn main()
}
}
match create_database()
match create_database().await
{
Ok(_) => {}
Err(e) => { eprintln!("Error: {}", e); }
Ok(_) =>
{}
Err(e) =>
{
eprintln!("Error: {}", e);
}
}
}
#[cfg(not(feature = "publisher"))]
pub fn main()
{
}