Add new data model

This commit is contained in:
Andras Schmelczer 2026-09-19 17:05:50 +01:00
commit ce5f3bbd7d

View file

@ -1,91 +1,125 @@
use base64::{Engine as _, engine::general_purpose::STANDARD};
use chrono::{DateTime, Utc};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use ts_rs::TS;
pub type VaultId = String;
pub type VaultUpdateId = i64;
pub type DocumentId = uuid::Uuid;
pub type UserId = String;
pub type DeviceId = String;
pub type FileManifestEntries = BTreeMap<DocumentId, String>;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, sqlx::FromRow)]
pub struct StoredDocumentVersion {
pub vault_update_id: VaultUpdateId,
#[sqlx(try_from = "uuid::fmt::Hyphenated")]
pub document_id: DocumentId,
pub relative_path: String,
pub updated_date: DateTime<Utc>,
pub content: Vec<u8>,
pub is_deleted: bool,
pub user_id: UserId,
pub device_id: DeviceId,
}
impl PartialEq<Self> for StoredDocumentVersion {
fn eq(&self, other: &Self) -> bool {
self.vault_update_id == other.vault_update_id
}
}
#[derive(TS, Debug, Clone, Serialize)]
#[derive(TS, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct DocumentVersionWithoutContent {
#[ts(as = "i32")]
#[ts(as = "f64")]
pub vault_update_id: VaultUpdateId,
pub document_id: DocumentId,
pub relative_path: String,
pub updated_date: DateTime<Utc>,
pub is_deleted: bool,
pub user_id: UserId,
pub device_id: DeviceId,
#[ts(as = "i32")]
pub content_size: u64,
pub content_size: usize,
}
impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
fn from(value: StoredDocumentVersion) -> Self {
impl From<&StoredDocumentVersion> for DocumentVersionWithoutContent {
fn from(v: &StoredDocumentVersion) -> Self {
Self {
vault_update_id: value.vault_update_id,
document_id: value.document_id,
relative_path: value.relative_path,
updated_date: value.updated_date,
is_deleted: value.is_deleted,
user_id: value.user_id,
device_id: value.device_id,
content_size: value.content.len() as u64,
vault_update_id: v.vault_update_id,
document_id: v.document_id,
updated_date: v.updated_date,
user_id: v.user_id.clone(),
device_id: v.device_id.clone(),
content_size: v.content.len(),
}
}
}
impl From<StoredDocumentVersion> for DocumentVersionWithoutContent {
fn from(v: StoredDocumentVersion) -> Self {
Self::from(&v)
}
}
#[derive(TS, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct DocumentVersion {
#[serde(flatten)]
pub metadata: DocumentVersionWithoutContent,
pub content_base64: String,
}
impl From<StoredDocumentVersion> for DocumentVersion {
fn from(v: StoredDocumentVersion) -> Self {
Self {
metadata: (&v).into(),
content_base64: STANDARD.encode(&v.content),
}
}
}
#[derive(TS, Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct FileManifest {
#[ts(as = "f64")]
pub file_manifest_id: VaultUpdateId,
#[ts(type = "Record<string, string>")]
pub entries: FileManifestEntries,
}
#[derive(TS, Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
#[ts(export)]
pub enum VaultEvent {
Content {
document: DocumentVersionWithoutContent,
},
FileManifest {
#[serde(rename = "fileManifest")]
#[ts(rename = "fileManifest")]
file_manifest: FileManifest,
},
}
#[derive(TS, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct EventRecord {
#[ts(as = "f64")]
pub event_id: VaultUpdateId,
pub request_id: uuid::Uuid,
#[serde(flatten)]
pub event: VaultEvent,
}
#[derive(TS, Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentVersion {
#[ts(as = "i32")]
pub vault_update_id: VaultUpdateId,
pub document_id: DocumentId,
pub relative_path: String,
pub updated_date: DateTime<Utc>,
pub content_base64: String,
pub is_deleted: bool,
pub user_id: UserId,
pub device_id: DeviceId,
#[ts(export)]
pub struct VaultSnapshot {
#[ts(as = "f64")]
pub head_event_id: VaultUpdateId,
pub file_manifest: FileManifest,
pub documents: Vec<DocumentVersionWithoutContent>,
}
impl From<StoredDocumentVersion> for DocumentVersion {
fn from(value: StoredDocumentVersion) -> Self {
Self {
vault_update_id: value.vault_update_id,
document_id: value.document_id,
relative_path: value.relative_path,
updated_date: value.updated_date,
content_base64: STANDARD.encode(&value.content),
is_deleted: value.is_deleted,
user_id: value.user_id,
device_id: value.device_id,
}
}
#[derive(TS, Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct EventBatch {
#[ts(as = "f64")]
pub head_event_id: VaultUpdateId,
pub events: Vec<EventRecord>,
}