POWR/docs/design/database_architecture.md

274 lines
6.4 KiB
Markdown
Raw Normal View History

2025-02-11 16:23:23 -05:00
# POWR Database Architecture
2025-02-11 10:38:17 -05:00
## 1. Entity Relationship Diagram
2025-02-11 16:23:23 -05:00
This diagram shows the core database structure and relationships between tables. The design supports both local-first operations with performance optimizations and Nostr protocol integration.
2025-02-11 10:38:17 -05:00
Key Features:
- Raw Nostr event storage in `nostr_events`
- Processed exercise data in `exercise_definitions`
2025-02-11 16:23:23 -05:00
- Media content storage in `exercise_media`
- Cache management in `cache_metadata`
2025-02-11 10:38:17 -05:00
- Dependency tracking in `incomplete_templates`
- Efficient tag indexing in `event_tags`
```mermaid
erDiagram
nostr_events ||--o{ event_tags : contains
nostr_events ||--o| exercise_definitions : processes
nostr_events ||--o| incomplete_templates : tracks
2025-02-11 16:23:23 -05:00
exercise_definitions ||--o{ exercise_media : stores
exercise_definitions ||--o{ cache_metadata : tracks
2025-02-11 10:38:17 -05:00
nostr_events {
string id PK
string pubkey
number kind
string raw_event
number created_at
number received_at
}
2025-02-11 16:23:23 -05:00
2025-02-11 10:38:17 -05:00
event_tags {
string event_id FK
string name
string value
number index
}
2025-02-11 16:23:23 -05:00
2025-02-11 10:38:17 -05:00
exercise_definitions {
string event_id FK
string title
string equipment
string format
string format_units
}
2025-02-11 16:23:23 -05:00
exercise_media {
string exercise_id FK
string media_type
blob content
blob thumbnail
number created_at
}
cache_metadata {
string content_id PK
string content_type
number last_accessed
number access_count
number priority
}
2025-02-11 10:38:17 -05:00
incomplete_templates {
string template_id FK
number missing_exercise_count
string missing_exercises
}
```
## 2. Event Processing Flow
2025-02-11 16:23:23 -05:00
This diagram illustrates how both local and Nostr events are processed, validated, and stored. The system handles Exercise Definitions (33401), Workout Templates (33402), and Workout Records (33403).
2025-02-11 10:38:17 -05:00
Key Features:
2025-02-11 16:23:23 -05:00
- Support for both local and Nostr events
- Unified validation process
- Media content handling
- Cache management
2025-02-11 10:38:17 -05:00
- Dependency checking
2025-02-11 16:23:23 -05:00
- Storage optimization
2025-02-11 10:38:17 -05:00
```mermaid
flowchart TB
subgraph Input
2025-02-11 16:23:23 -05:00
A[New Event] --> B{Source}
B -->|Local| C[Local Creation]
B -->|Nostr| D[Nostr Event]
C --> E{Event Type}
D --> E
2025-02-11 10:38:17 -05:00
end
2025-02-11 16:23:23 -05:00
E -->|kind 33401| F[Exercise Definition]
E -->|kind 33402| G[Workout Template]
E -->|kind 33403| H[Workout Record]
2025-02-11 10:38:17 -05:00
subgraph Processing
2025-02-11 16:23:23 -05:00
F --> I[Validate Event]
G --> I
H --> I
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
I --> J{Valid?}
J -->|No| K[Reject Event]
J -->|Yes| L[Store Raw Event]
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
L --> M[Process Event]
M --> N{Has Media?}
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
N -->|Yes| O[Process Media]
N -->|No| P{Dependencies?}
O --> P
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
P -->|Missing| Q[Store as Incomplete]
P -->|Complete| R[Store Processed Data]
Q --> S[Queue Missing Events]
R --> T[Update Cache]
2025-02-11 10:38:17 -05:00
end
subgraph Storage
2025-02-11 16:23:23 -05:00
R --> U[(NostrEvents)]
R --> V[(ProcessedData)]
O --> W[(MediaStore)]
T --> X[(Cache)]
Q --> Y[(IncompleteQueue)]
2025-02-11 10:38:17 -05:00
end
```
## 3. Query and Cache Flow
2025-02-11 16:23:23 -05:00
This sequence diagram shows how data is retrieved, using a performance-optimized approach with LRU caching, efficient media handling, and template dependency resolution.
2025-02-11 10:38:17 -05:00
Key Features:
2025-02-11 16:23:23 -05:00
- Smart cache management
- Media streaming
2025-02-11 10:38:17 -05:00
- Template dependency resolution
2025-02-11 16:23:23 -05:00
- Query optimization
- Priority-based caching
2025-02-11 10:38:17 -05:00
```mermaid
sequenceDiagram
2025-02-11 16:23:23 -05:00
participant UI as UI Layer
2025-02-11 10:38:17 -05:00
participant Cache as LRU Cache
2025-02-11 16:23:23 -05:00
participant Media as Media Store
2025-02-11 10:38:17 -05:00
participant DB as SQLite
2025-02-11 16:23:23 -05:00
participant Query as Query Builder
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
UI->>Query: Request Content
Query->>Cache: Check Cache
2025-02-11 10:38:17 -05:00
alt Cache Hit
2025-02-11 16:23:23 -05:00
Cache-->>UI: Return Cached Data
opt Has Media References
UI->>Media: Request Media
Media-->>UI: Stream Media
end
else Cache Miss
Query->>DB: Query Database
DB-->>Query: Raw Results
opt Has Media
Query->>Media: Load Media
Media-->>Query: Media Content
end
Query->>Query: Process Results
Query->>Cache: Update Cache
Query-->>UI: Return Results
2025-02-11 10:38:17 -05:00
end
2025-02-11 16:23:23 -05:00
Note over Query,DB: Template Resolution
2025-02-11 10:38:17 -05:00
2025-02-11 16:23:23 -05:00
opt Template Dependencies
Query->>DB: Check Dependencies
alt Missing Dependencies
DB-->>Query: Missing References
Query-->>UI: Return Incomplete
else Complete
DB-->>Query: All Dependencies
Query-->>UI: Return Complete
end
2025-02-11 10:38:17 -05:00
end
```
## 4. Component Architecture
2025-02-11 16:23:23 -05:00
This diagram shows the application architecture, focusing on the interaction between local-first operations and Nostr integration.
2025-02-11 10:38:17 -05:00
Key Features:
2025-02-11 16:23:23 -05:00
- Local-first prioritization
- Efficient service layers
- Clear data boundaries
- Performance optimization
- NDK integration points
2025-02-11 10:38:17 -05:00
```mermaid
graph TB
subgraph UI Layer
2025-02-11 16:23:23 -05:00
A[Views]
B[Forms]
C[Media Display]
2025-02-11 10:38:17 -05:00
end
subgraph Service Layer
D[Library Service]
E[Event Processor]
F[Cache Manager]
2025-02-11 16:23:23 -05:00
G[Media Service]
2025-02-11 10:38:17 -05:00
end
2025-02-11 16:23:23 -05:00
subgraph Storage Layer
H[(SQLite)]
I[Media Store]
J[Event Store]
K[Query Builder]
2025-02-11 10:38:17 -05:00
end
2025-02-11 16:23:23 -05:00
subgraph NDK Layer
L[Relay Manager]
M[Event Publisher]
N[Sync Manager]
2025-02-11 10:38:17 -05:00
end
A --> D
B --> D
2025-02-11 16:23:23 -05:00
C --> G
2025-02-11 10:38:17 -05:00
D --> E
D --> F
2025-02-11 16:23:23 -05:00
E --> H
E --> J
2025-02-11 10:38:17 -05:00
F --> H
2025-02-11 16:23:23 -05:00
G --> I
2025-02-11 10:38:17 -05:00
D -.-> L
2025-02-11 16:23:23 -05:00
D -.-> M
H --> K
K --> F
2025-02-11 10:38:17 -05:00
```
## Implementation Notes
2025-02-11 16:23:23 -05:00
These diagrams represent POWR's database implementation with a focus on local-first performance while maintaining Nostr compatibility.
1. **Local-First Design**
- SQLite as primary storage
- Efficient caching layer
- Optimized media handling
- Smart query patterns
- Background processing
2. **Nostr Integration**
- Raw event preservation
- NDK compatibility
- Event validation
- Dependency tracking
- Sync management
3. **Performance Features**
- LRU caching with priorities
- Media optimization
- Query optimization
- Batch processing
- Background sync
4. **Data Integrity**
- Transaction management
- Dependency tracking
- Event validation
- Error handling
- Recovery procedures