diff --git a/README.md b/README.md index 57e9874..d7ff104 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,6 @@ Create a OAuth client at [google](https://console.cloud.google.com/) Create vapid keys for webpush this can be done via `npx web-push generate-vapid-keys` -When using the default sqliteDb create a `.data` folder in the root of the project. - ```dotenv #Nuxt NUXT_GOOGLE_CLIENT_ID= @@ -28,7 +26,6 @@ API_WEBPUSH_PRIVATE_KEY= run `just dev` to start the nuxt dev server and the api server - ## Deployment via Nix ### flake.nix diff --git a/api/orm/src/lib.rs b/api/orm/src/lib.rs index 0f367e1..134f032 100644 --- a/api/orm/src/lib.rs +++ b/api/orm/src/lib.rs @@ -1,3 +1,5 @@ +use std::{fs, io, path::Path}; + use migration::{Migrator, MigratorTrait}; use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; use thiserror::Error; @@ -12,9 +14,24 @@ pub enum OrmError { DbError(#[from] DbErr), #[error("unsupported database backend")] UnsupportedBackend, + #[error("failed to create sqlite folder")] + FailedToCreateFolder(#[from] io::Error), } pub async fn init_db(url: &str) -> Result { + // If using SQLite, extract the file path and create parent directories + if let Some(path_str) = url.strip_prefix("sqlite:") { + let clean_path = path_str.split('?').next().unwrap_or(path_str); + + let file_path = clean_path.strip_prefix("//").unwrap_or(clean_path); + + if let Some(parent) = Path::new(file_path).parent() + && !parent.as_os_str().is_empty() + { + fs::create_dir_all(parent).map_err(OrmError::FailedToCreateFolder)?; + } + } + let mut options = ConnectOptions::new(url); options.sqlx_logging(false); diff --git a/server/utils/auth.ts b/server/utils/auth.ts index 51e2ac1..9aec939 100644 --- a/server/utils/auth.ts +++ b/server/utils/auth.ts @@ -1,3 +1,5 @@ +import fs from "node:fs"; +import path from "node:path"; import { DatabaseSync } from "node:sqlite"; import { apiKey } from "@better-auth/api-key"; import { betterAuth } from "better-auth"; @@ -14,6 +16,11 @@ export function getDatabaseConnection() { } const sqlitePath = dbUrl.replace(/^(sqlite:|file:)/, ""); + const dir = path.dirname(sqlitePath); + if (dir && dir !== ".") { + fs.mkdirSync(dir, { recursive: true }); + } + return new DatabaseSync(sqlitePath); }