add account and account_category entities with relations
This commit is contained in:
36
src-tauri/src/db/entities/account.rs
Normal file
36
src-tauri/src/db/entities/account.rs
Normal file
@@ -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<super::account_category::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::AccountCategory.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
30
src-tauri/src/db/entities/account_category.rs
Normal file
30
src-tauri/src/db/entities/account_category.rs
Normal file
@@ -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<super::account::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Account.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
@@ -1,3 +1,6 @@
|
||||
//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod account;
|
||||
pub mod account_category;
|
||||
|
||||
@@ -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;
|
||||
|
||||
85
src-tauri/src/db/mod.rs
Normal file
85
src-tauri/src/db/mod.rs
Normal file
@@ -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<S: StatementBuilder>(&self, stmt: &S) -> Result<ExecResult, DbErr> {
|
||||
match self {
|
||||
Db::Connection(conn) => conn.execute(stmt).await,
|
||||
Db::Transaction(txn) => txn.execute(stmt).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn query_one<S: StatementBuilder>(&self, stmt: &S) -> Result<Option<QueryResult>, DbErr> {
|
||||
match self {
|
||||
Db::Connection(conn) => conn.query_one(stmt).await,
|
||||
Db::Transaction(txn) => txn.query_one(stmt).await,
|
||||
}
|
||||
}
|
||||
|
||||
async fn query_all<S: StatementBuilder>(&self, stmt: &S) -> Result<Vec<QueryResult>, 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<Option<QueryResult>, 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<Vec<QueryResult>, 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<ExecResult, DbErr> {
|
||||
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<ExecResult, DbErr> {
|
||||
match self {
|
||||
Db::Connection(conn) => conn.execute_unprepared(sql).await,
|
||||
Db::Transaction(txn) => txn.execute_unprepared(sql).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user