diff --git a/src-tauri/src/db/entities/account.rs b/src-tauri/src/db/entities/account.rs new file mode 100644 index 0000000..77bc27e --- /dev/null +++ b/src-tauri/src/db/entities/account.rs @@ -0,0 +1,36 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "account")] +#[allow(dead_code)] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i64, + pub name: String, + pub account_category_id: i64, + pub created_at: String, + pub updated_at: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::account_category::Entity", + from = "Column::AccountCategoryId", + to = "super::account_category::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + AccountCategory, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::AccountCategory.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entities/account_category.rs b/src-tauri/src/db/entities/account_category.rs new file mode 100644 index 0000000..1ea9baf --- /dev/null +++ b/src-tauri/src/db/entities/account_category.rs @@ -0,0 +1,30 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)] +#[sea_orm(table_name = "account_category")] +#[allow(dead_code)] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i64, + pub name: String, + pub account_type: String, + pub created_at: String, + pub updated_at: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::account::Entity")] + Account, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Account.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entities/mod.rs b/src-tauri/src/db/entities/mod.rs index e284bbf..d72fe24 100644 --- a/src-tauri/src/db/entities/mod.rs +++ b/src-tauri/src/db/entities/mod.rs @@ -1,3 +1,6 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 pub mod prelude; + +pub mod account; +pub mod account_category; diff --git a/src-tauri/src/db/entities/prelude.rs b/src-tauri/src/db/entities/prelude.rs index acf76ef..453e3eb 100644 --- a/src-tauri/src/db/entities/prelude.rs +++ b/src-tauri/src/db/entities/prelude.rs @@ -1,3 +1,6 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 2.0 #![allow(unused_imports)] + +pub use super::account::Entity as Account; +pub use super::account_category::Entity as AccountCategory; diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs new file mode 100644 index 0000000..b338ed2 --- /dev/null +++ b/src-tauri/src/db/mod.rs @@ -0,0 +1,85 @@ +pub mod entities; + +use async_trait::async_trait; +use sea_orm::{ + ConnectionTrait, DatabaseConnection, DatabaseTransaction, DbBackend, DbErr, ExecResult, + QueryResult, Statement, StatementBuilder, +}; + +// 1. Create a uniform enum wrapper +pub enum Db<'a> { + Connection(&'a DatabaseConnection), + Transaction(&'a DatabaseTransaction), +} + +impl<'a> From<&'a DatabaseConnection> for Db<'a> { + fn from(conn: &'a DatabaseConnection) -> Self { + Db::Connection(conn) + } +} + +impl<'a> From<&'a DatabaseTransaction> for Db<'a> { + fn from(txn: &'a DatabaseTransaction) -> Self { + Db::Transaction(txn) + } +} + +// 2. Implement ConnectionTrait by forwarding down to the inner variants +#[async_trait] +impl<'a> ConnectionTrait for Db<'a> { + fn get_database_backend(&self) -> DbBackend { + match self { + Db::Connection(conn) => conn.get_database_backend(), + Db::Transaction(txn) => txn.get_database_backend(), + } + } + + async fn execute(&self, stmt: &S) -> Result { + match self { + Db::Connection(conn) => conn.execute(stmt).await, + Db::Transaction(txn) => txn.execute(stmt).await, + } + } + + async fn query_one(&self, stmt: &S) -> Result, DbErr> { + match self { + Db::Connection(conn) => conn.query_one(stmt).await, + Db::Transaction(txn) => txn.query_one(stmt).await, + } + } + + async fn query_all(&self, stmt: &S) -> Result, DbErr> { + match self { + Db::Connection(conn) => conn.query_all(stmt).await, + Db::Transaction(txn) => txn.query_all(stmt).await, + } + } + + async fn query_one_raw(&self, stmt: Statement) -> Result, DbErr> { + match self { + Db::Connection(conn) => conn.query_one_raw(stmt).await, + Db::Transaction(txn) => txn.query_one_raw(stmt).await, + } + } + + async fn query_all_raw(&self, stmt: Statement) -> Result, DbErr> { + match self { + Db::Connection(conn) => conn.query_all_raw(stmt).await, + Db::Transaction(txn) => txn.query_all_raw(stmt).await, + } + } + + async fn execute_raw(&self, stmt: Statement) -> Result { + match self { + Db::Connection(conn) => conn.execute_raw(stmt).await, + Db::Transaction(txn) => txn.execute_raw(stmt).await, + } + } + + async fn execute_unprepared(&self, sql: &str) -> Result { + match self { + Db::Connection(conn) => conn.execute_unprepared(sql).await, + Db::Transaction(txn) => txn.execute_unprepared(sql).await, + } + } +}