diff --git a/Cargo.lock b/Cargo.lock index de3c5e5..fea2d07 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1199,19 +1199,6 @@ dependencies = [ "parking_lot_core", ] -[[package]] -name = "db" -version = "0.1.0" -dependencies = [ - "anyhow", - "migration", - "rand 0.10.0", - "sea-orm", - "thiserror 2.0.18", - "tokio", - "tracing", -] - [[package]] name = "deltae" version = "0.3.2" @@ -1363,6 +1350,19 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "dto" +version = "0.1.0" +dependencies = [ + "anyhow", + "migration", + "rand 0.10.0", + "sea-orm", + "thiserror 2.0.18", + "tokio", + "tracing", +] + [[package]] name = "dunce" version = "1.0.5" @@ -1501,8 +1501,8 @@ dependencies = [ "clap", "color-eyre", "crossterm", - "db", "directories", + "dto", "futures", "human-panic", "kdl", diff --git a/Cargo.toml b/Cargo.toml index ef1254f..8f408d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] members = [ - "crates/db", - "crates/db/migration", + "crates/dto", + "crates/dto/migration", "crates/tree" ] @@ -72,7 +72,7 @@ tracing-subscriber = { version = "0.3.22", features = ["env-filter"] } tracing-error = "0.2.1" clap = { version = "4.5.60", features = ["derive", "cargo", "wrap_help", "unicode", "string", "unstable-styles"] } kdl = "6.5.0" -db = {path="./crates/db"} +dto = {path="./crates/dto"} [build-dependencies] anyhow = "1.0.102" diff --git a/crates/db/src/errors.rs b/crates/db/src/errors.rs deleted file mode 100644 index fb312fb..0000000 --- a/crates/db/src/errors.rs +++ /dev/null @@ -1,11 +0,0 @@ -use thiserror::Error; - -pub type DbResult = Result; - -#[derive(Debug, Error)] -pub enum DbError { - #[error("database file not found, tried looking at {not_found_at}")] - NotFound { not_found_at: String }, - #[error("Seaorm Error")] - SeaOrm(#[from] sea_orm::error::DbErr), -} diff --git a/crates/db/Cargo.toml b/crates/dto/Cargo.toml similarity index 97% rename from crates/db/Cargo.toml rename to crates/dto/Cargo.toml index e593e92..05e1692 100644 --- a/crates/db/Cargo.toml +++ b/crates/dto/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "db" +name = "dto" version.workspace = true edition.workspace = true authors.workspace = true diff --git a/crates/db/migration/Cargo.toml b/crates/dto/migration/Cargo.toml similarity index 100% rename from crates/db/migration/Cargo.toml rename to crates/dto/migration/Cargo.toml diff --git a/crates/db/migration/README.md b/crates/dto/migration/README.md similarity index 100% rename from crates/db/migration/README.md rename to crates/dto/migration/README.md diff --git a/crates/db/migration/src/lib.rs b/crates/dto/migration/src/lib.rs similarity index 100% rename from crates/db/migration/src/lib.rs rename to crates/dto/migration/src/lib.rs diff --git a/crates/db/migration/src/m20260318_233726_group_table.rs b/crates/dto/migration/src/m20260318_233726_group_table.rs similarity index 100% rename from crates/db/migration/src/m20260318_233726_group_table.rs rename to crates/dto/migration/src/m20260318_233726_group_table.rs diff --git a/crates/db/migration/src/m20260319_002245_task_table.rs b/crates/dto/migration/src/m20260319_002245_task_table.rs similarity index 100% rename from crates/db/migration/src/m20260319_002245_task_table.rs rename to crates/dto/migration/src/m20260319_002245_task_table.rs diff --git a/crates/dto/migration/src/m20260323_002518_zettel_table.rs b/crates/dto/migration/src/m20260323_002518_zettel_table.rs new file mode 100644 index 0000000..ba300f1 --- /dev/null +++ b/crates/dto/migration/src/m20260323_002518_zettel_table.rs @@ -0,0 +1,58 @@ +use sea_orm_migration::{prelude::*, schema::*}; + +use crate::types::NANO_ID_LEN; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(Zettel::Table) + .if_not_exists() + .col(pk_auto(Zettel::Id)) + .col( + string(Zettel::NanoId) + .string_len(NANO_ID_LEN as u32) + .unique_key() + .not_null(), + ) + .col(string(Zettel::Title).not_null()) + .col(string(Zettel::FilePath).not_null()) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .name("idx_zettel_pub_id") + .table(Zettel::Table) + .col(Zettel::NanoId) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_index(Index::drop().name("idx_zettel_pub_id").to_owned()) + .await?; + + manager + .drop_table(Table::drop().table("post").to_owned()) + .await + } +} + +#[derive(DeriveIden)] +pub enum Zettel { + Table, + Id, + NanoId, + Title, + FilePath, +} diff --git a/crates/db/migration/src/main.rs b/crates/dto/migration/src/main.rs similarity index 100% rename from crates/db/migration/src/main.rs rename to crates/dto/migration/src/main.rs diff --git a/crates/db/migration/src/types/mod.rs b/crates/dto/migration/src/types/mod.rs similarity index 100% rename from crates/db/migration/src/types/mod.rs rename to crates/dto/migration/src/types/mod.rs diff --git a/crates/db/migration/src/types/nano_id.rs b/crates/dto/migration/src/types/nano_id.rs similarity index 100% rename from crates/db/migration/src/types/nano_id.rs rename to crates/dto/migration/src/types/nano_id.rs diff --git a/crates/db/migration/src/types/priority.rs b/crates/dto/migration/src/types/priority.rs similarity index 100% rename from crates/db/migration/src/types/priority.rs rename to crates/dto/migration/src/types/priority.rs diff --git a/crates/db/src/lib.rs b/crates/dto/src/db.rs similarity index 79% rename from crates/db/src/lib.rs rename to crates/dto/src/db.rs index f108f61..b940145 100644 --- a/crates/db/src/lib.rs +++ b/crates/dto/src/db.rs @@ -1,27 +1,10 @@ -//! The database abstraction for the different actions `Filaments` requires -//! from a database service. - use std::path::PathBuf; -use migration::{Migrator, MigratorTrait}; +use migration::{Migrator, MigratorTrait as _}; use sea_orm::{Database, DatabaseConnection}; use tracing::debug; -use crate::errors::{DbError, DbResult}; - -/// Database Errors -mod errors; - -/// Database entities -pub mod entity; - -/// Types defined in migration -pub use migration::types::*; - -pub use sea_orm::ActiveValue; - -#[expect(unused_imports)] -pub use errors::*; +use thiserror::Error; /// Database struct #[derive(Debug)] @@ -29,6 +12,16 @@ pub struct Db { conn: DatabaseConnection, } +pub type DbResult = Result; + +#[derive(Debug, Error)] +pub enum DbError { + #[error("database file not found, tried looking at {not_found_at}")] + NotFound { not_found_at: String }, + #[error("Seaorm Error")] + SeaOrm(#[from] sea_orm::error::DbErr), +} + impl AsRef for Db { fn as_ref(&self) -> &DatabaseConnection { &self.conn diff --git a/crates/db/src/entity/group.rs b/crates/dto/src/entity/group.rs similarity index 100% rename from crates/db/src/entity/group.rs rename to crates/dto/src/entity/group.rs diff --git a/crates/db/src/entity/mod.rs b/crates/dto/src/entity/mod.rs similarity index 99% rename from crates/db/src/entity/mod.rs rename to crates/dto/src/entity/mod.rs index 0cdf054..e2dbb39 100644 --- a/crates/db/src/entity/mod.rs +++ b/crates/dto/src/entity/mod.rs @@ -1,7 +1,6 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 pub mod prelude; - pub mod group; pub mod task; pub mod zettel; diff --git a/crates/db/src/entity/prelude.rs b/crates/dto/src/entity/prelude.rs similarity index 86% rename from crates/db/src/entity/prelude.rs rename to crates/dto/src/entity/prelude.rs index 2282eae..373adbb 100644 --- a/crates/db/src/entity/prelude.rs +++ b/crates/dto/src/entity/prelude.rs @@ -1,4 +1,5 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 +#![expect(unused_imports)] pub use super::group::Entity as Group; pub use super::task::Entity as Task; diff --git a/crates/db/src/entity/task.rs b/crates/dto/src/entity/task.rs similarity index 100% rename from crates/db/src/entity/task.rs rename to crates/dto/src/entity/task.rs diff --git a/crates/db/src/entity/zettel.rs b/crates/dto/src/entity/zettel.rs similarity index 100% rename from crates/db/src/entity/zettel.rs rename to crates/dto/src/entity/zettel.rs diff --git a/crates/dto/src/lib.rs b/crates/dto/src/lib.rs new file mode 100644 index 0000000..37f37e2 --- /dev/null +++ b/crates/dto/src/lib.rs @@ -0,0 +1,39 @@ +//! The DTO's (Data Transfer Objects) used to interact with +//! the Database. There is also a simple database struct in here. + +/// Database and its Errors +mod db; +pub use db::*; + +/// exported traits for the database +pub use sea_orm::ActiveModelTrait; +pub use sea_orm::ActiveValue; + +/// Exporting this as a generic NanoId. +pub use migration::types::NanoId; +/// Exporting this as DTO so we can newtype this in a later crate +/// and add additional functionality to it. +pub use migration::types::Priority as PriorityDTO; + +mod entity; + +/// Everything related to groups. +pub use entity::group::ActiveModel as GroupActiveModel; +pub use entity::group::ActiveModelEx as GroupActiveModelEx; +pub use entity::group::Entity as GroupEntity; +pub use entity::group::Model as GroupModel; +pub use entity::group::ModelEx as GroupModelEx; + +/// Everything related to tasks. +pub use entity::task::ActiveModel as TaskActiveModel; +pub use entity::task::ActiveModelEx as TaskActiveModelEx; +pub use entity::task::Entity as TaskEntity; +pub use entity::task::Model as TaskModel; +pub use entity::task::ModelEx as TaskModelEx; + +/// Everything related to zetetl's. +pub use entity::zettel::ActiveModel as ZettelActiveModel; +pub use entity::zettel::ActiveModelEx as ZettelActiveModelEx; +pub use entity::zettel::Entity as ZettelEntity; +pub use entity::zettel::Model as ZettelModel; +pub use entity::zettel::Model as ZettelModelEx; diff --git a/crates/db/tests/common/mod.rs b/crates/dto/tests/common/mod.rs similarity index 97% rename from crates/db/tests/common/mod.rs rename to crates/dto/tests/common/mod.rs index 6537e30..3c0d71a 100644 --- a/crates/db/tests/common/mod.rs +++ b/crates/dto/tests/common/mod.rs @@ -3,7 +3,7 @@ use std::{ path::PathBuf, }; -use db::Db; +use dto::Db; use rand::RngExt; pub async fn fresh_test_db() -> Db { diff --git a/crates/db/tests/task.rs b/crates/dto/tests/task.rs similarity index 70% rename from crates/db/tests/task.rs rename to crates/dto/tests/task.rs index b24b55c..202d6f6 100644 --- a/crates/db/tests/task.rs +++ b/crates/dto/tests/task.rs @@ -1,15 +1,16 @@ //! Testing task functionality with the database abstraction. -use db::entity::{group, prelude::*, zettel}; -use db::{ActiveValue::Set, entity::task}; -use sea_orm::ActiveModelTrait; +use dto::{ + ActiveModelTrait as _, ActiveValue::Set, GroupActiveModel, GroupEntity, GroupModel, + TaskActiveModel, TaskEntity, TaskModel, ZettelActiveModel, ZettelEntity, ZettelModel, +}; mod common; #[tokio::test] async fn test_group_task_insert() { let db = common::fresh_test_db().await; - let group_zettel: zettel::Model = zettel::ActiveModel { + let group_zettel: ZettelModel = ZettelActiveModel { title: Set("Something".to_owned()), file_path: Set("/voo/doo".to_owned()), ..Default::default() @@ -18,7 +19,7 @@ async fn test_group_task_insert() { .await .unwrap(); - let group: group::Model = group::ActiveModel { + let group: GroupModel = GroupActiveModel { name: Set("something".to_owned()), color: Set("color".to_owned()), zettel_id: Set(group_zettel.nano_id.clone()), @@ -28,7 +29,7 @@ async fn test_group_task_insert() { .await .unwrap(); - let task_zettel: zettel::Model = zettel::ActiveModel { + let task_zettel: ZettelModel = ZettelActiveModel { // nano_id: Set(NanoId::default()), title: Set("nomething".to_owned()), file_path: Set("/voo/doo".to_owned()), @@ -38,7 +39,7 @@ async fn test_group_task_insert() { .await .unwrap(); - let task: task::Model = task::ActiveModel { + let task: TaskModel = TaskActiveModel { name: Set("something".to_owned()), group_id: Set(group.nano_id.to_owned()), zettel_id: Set(task_zettel.nano_id.clone()), @@ -48,19 +49,19 @@ async fn test_group_task_insert() { .await .unwrap(); - let task = Task::load() + let task = TaskEntity::load() .filter_by_nano_id(task.nano_id.clone()) - .with(Group) - .with(Zettel) + .with(GroupEntity) + .with(ZettelEntity) .one(db.as_ref()) .await .unwrap() .unwrap(); - let group = Group::load() + let group = GroupEntity::load() .filter_by_nano_id(group.nano_id.clone()) - .with(Task) - .with(Zettel) + .with(TaskEntity) + .with(ZettelEntity) .one(db.as_ref()) .await .unwrap() @@ -68,6 +69,4 @@ async fn test_group_task_insert() { println!("group: {group:#?}"); println!("task: {task:#?}"); - - panic!() } diff --git a/flake.lock b/flake.lock index 6d95aa3..501ddc5 100644 --- a/flake.lock +++ b/flake.lock @@ -8,11 +8,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1774163246, - "narHash": "sha256-gzlqyLjP44LWraUd3Zn4xrQKOtK+zcBJ77pnsSUsxcM=", + "lastModified": 1774555695, + "narHash": "sha256-JpTx7Rn8sILuXAH9a0K0UCZST5KY9ZTMzrZ61KcsNno=", "owner": "nix-community", "repo": "fenix", - "rev": "4cd28929c68cae521589bc21958d3793904ed1e2", + "rev": "d76ca95395662ed18b02b894e487eb41fd0e7d99", "type": "github" }, "original": { @@ -23,12 +23,12 @@ }, "nixpkgs": { "locked": { - "lastModified": 1773821835, - "narHash": "sha256-TJ3lSQtW0E2JrznGVm8hOQGVpXjJyXY2guAxku2O9A4=", - "rev": "b40629efe5d6ec48dd1efba650c797ddbd39ace0", - "revCount": 964859, + "lastModified": 1774386573, + "narHash": "sha256-4hAV26quOxdC6iyG7kYaZcM3VOskcPUrdCQd/nx8obc=", + "rev": "46db2e09e1d3f113a13c0d7b81e2f221c63b8ce9", + "revCount": 969196, "type": "tarball", - "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.964859%2Brev-b40629efe5d6ec48dd1efba650c797ddbd39ace0/019d03e9-2959-7276-adaa-d074e96422de/source.tar.gz" + "url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.969196%2Brev-46db2e09e1d3f113a13c0d7b81e2f221c63b8ce9/019d279e-af65-79ce-92be-5dee7b1e36d4/source.tar.gz" }, "original": { "type": "tarball", @@ -44,11 +44,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1774097238, - "narHash": "sha256-hcujm/qEX4RUybdBCrQKdQNqTRYDItmnbjJRP5ky5vc=", + "lastModified": 1774454876, + "narHash": "sha256-bwkM8HseUs/22x+hy6FWvJMP6q/2CKBrm4sYxz9rMY8=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "76de1de27c0ca1329bc41324edab22c82d69e779", + "rev": "9253d39eab8b9c9da3c1412fc94764e01d55a02b", "type": "github" }, "original": {