From 836178ca45c2f374b7ea13967c60577d5c8d170c Mon Sep 17 00:00:00 2001 From: Jamie Date: Fri, 24 Dec 2021 22:51:13 -0800 Subject: [PATCH] better relations --- packages/core/lib/db/entity/file.rs | 129 +++++++++++---------------- packages/core/lib/db/entity/space.rs | 25 ++++-- packages/core/lib/db/entity/tag.rs | 25 ++++-- packages/core/lib/tx/type.rs | 39 ++++++-- 4 files changed, 117 insertions(+), 101 deletions(-) diff --git a/packages/core/lib/db/entity/file.rs b/packages/core/lib/db/entity/file.rs index eea0c7472..ca5395e36 100644 --- a/packages/core/lib/db/entity/file.rs +++ b/packages/core/lib/db/entity/file.rs @@ -9,85 +9,64 @@ use serde::{Deserialize, Serialize}; #[sea_orm(table_name = "files")] // ------------------------------------- pub struct Model { - // identity - #[sea_orm(primary_key)] - pub id: u32, - // pub buffer_checksum: String, - #[sea_orm(unique)] - pub meta_checksum: String, - pub uri: String, - pub is_dir: bool, - // date - pub date_created: Option, - pub date_modified: Option, - pub date_indexed: Option, - // metadata - pub name: String, - pub extension: String, - pub size_in_bytes: String, - pub library_id: u32, - // #[sea_orm(column_type = "Int")] - // pub encryption: crypto::Encryption, - // ownership - #[sea_orm(nullable)] - pub ipfs_id: Option, - #[sea_orm(nullable)] - pub storage_device_id: Option, - #[sea_orm(nullable)] - pub capture_device_id: Option, - #[sea_orm(nullable)] - pub parent_id: Option, + // identity + #[sea_orm(primary_key)] + pub id: u32, + // pub buffer_checksum: String, + #[sea_orm(unique)] + pub meta_checksum: String, + pub uri: String, + pub is_dir: bool, + // date + pub date_created: Option, + pub date_modified: Option, + pub date_indexed: Option, + // metadata + pub name: String, + pub extension: String, + pub size_in_bytes: String, + pub library_id: u32, + // #[sea_orm(column_type = "Int")] + // pub encryption: crypto::Encryption, + // ownership + #[sea_orm(nullable)] + pub ipfs_id: Option, + + #[sea_orm(nullable)] + pub storage_device_id: Option, + + #[sea_orm(nullable)] + pub capture_device_id: Option, + + #[sea_orm(nullable)] + pub parent_id: Option, } -#[derive(Copy, Clone, Debug, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { - Library, - StorageDevice, - CaptureDevice, - ParentFile, -} + #[sea_orm( + belongs_to = "super::library::Entity", + from = "Column::LibraryId", + to = "super::library::Column::Id" + )] + Library, -impl RelationTrait for Relation { - fn def(&self) -> RelationDef { - match self { - Self::Library => Entity::belongs_to(super::library::Entity) - .from(Column::LibraryId) - .to(super::library::Column::Id) - .into(), - Self::StorageDevice => Entity::belongs_to(super::storage_device::Entity) - .from(Column::StorageDeviceId) - .to(super::storage_device::Column::Id) - .into(), - Self::CaptureDevice => Entity::belongs_to(super::capture_device::Entity) - .from(Column::CaptureDeviceId) - .to(super::capture_device::Column::Id) - .into(), - Self::ParentFile => Entity::belongs_to(Entity) - .from(Column::ParentId) - .to(Column::Id) - .into(), - } - } -} -impl Related for Entity { - fn to() -> RelationDef { - Relation::Library.def() - } -} -impl Related for Entity { - fn to() -> RelationDef { - Relation::StorageDevice.def() - } -} -impl Related for Entity { - fn to() -> RelationDef { - Relation::CaptureDevice.def() - } -} -impl Related for Entity { - fn to() -> RelationDef { - Relation::ParentFile.def() - } + #[sea_orm( + belongs_to = "super::storage_device::Entity", + from = "Column::StorageDeviceId", + to = "super::storage_device::Column::Id" + )] + StorageDevice, + + #[sea_orm( + belongs_to = "super::capture_device::Entity", + from = "Column::CaptureDeviceId", + to = "super::capture_device::Column::Id" + )] + CaptureDevice, + + #[sea_orm(belongs_to = "Entity", from = "Column::ParentId", to = "Column::Id")] + ParentFile, } impl ActiveModelBehavior for ActiveModel {} diff --git a/packages/core/lib/db/entity/space.rs b/packages/core/lib/db/entity/space.rs index 1823bb29a..3fa2c1948 100644 --- a/packages/core/lib/db/entity/space.rs +++ b/packages/core/lib/db/entity/space.rs @@ -8,17 +8,24 @@ use serde::{Deserialize, Serialize}; #[sea_orm(table_name = "spaces")] // ------------------------------------- pub struct Model { - #[sea_orm(primary_key)] - pub id: u32, - pub name: String, - pub calculated_size_in_bytes: Option, - pub calculated_file_count: Option, - pub library_id: String, - pub date_created: Option, - pub date_modified: Option, + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub calculated_size_in_bytes: Option, + pub calculated_file_count: Option, + pub library_id: String, + pub date_created: Option, + pub date_modified: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::library::Entity", + from = "Column::LibraryId", + to = "super::library::Column::Id" + )] + Library, +} impl ActiveModelBehavior for ActiveModel {} diff --git a/packages/core/lib/db/entity/tag.rs b/packages/core/lib/db/entity/tag.rs index 6e6eb28b7..50ba7100a 100644 --- a/packages/core/lib/db/entity/tag.rs +++ b/packages/core/lib/db/entity/tag.rs @@ -8,17 +8,24 @@ use serde::{Deserialize, Serialize}; #[sea_orm(table_name = "tags")] // ------------------------------------- pub struct Model { - #[sea_orm(primary_key)] - pub id: u32, - pub name: String, - pub total_files: Option, - pub redundancy_goal: Option, - pub library_id: String, - pub date_created: Option, - pub date_modified: Option, + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub total_files: Option, + pub redundancy_goal: Option, + pub library_id: String, + pub date_created: Option, + pub date_modified: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm( + belongs_to = "super::library::Entity", + from = "Column::LibraryId", + to = "super::library::Column::Id" + )] + Library, +} impl ActiveModelBehavior for ActiveModel {} diff --git a/packages/core/lib/tx/type.rs b/packages/core/lib/tx/type.rs index e2f1490da..2b338bfa4 100644 --- a/packages/core/lib/tx/type.rs +++ b/packages/core/lib/tx/type.rs @@ -1,13 +1,36 @@ -pub enum TransactionType { +// +// Transactions are used to describe changes to be made to the database +// - a transaction must be run as a single read, write, or delete operation to the SQLite database +// - they are emitted by a given client and accepted or rejected by sister clients +// - if a client rejects a transaction the entire database will be marked for re-sync +pub struct Transaction { + pub id: i32, + pub timestamp: i32, // unix timestamp + pub client_id: i32, // the client that created the transaction + + pub model: String, // the model that the transaction is for + pub method TransactionMethod, + + // vector of transaction entries + pub mutations: Option> +} + +// +pub struct ObjectMutation { + pub primary_key: Vec, + pub columns: Vec, + pub new_values: Vec, +} + +pub enum TransactionMethod { CREATE, UPDATE, DELETE, } -pub struct Transaction { - pub id: i32, - pub timestamp: i32, - pub client_id: i32, - pub target_client_id: i32, - // type TransactionType -} +// create tag +// update tag +// assign tag to file +// create files +// create action records +// \ No newline at end of file