MikroORM Best Practices - Entity Relations & Unit of Work Refactoring #267
Labels
No labels
good first issue
has-pr
help wanted
idea
priority
critical
priority
high
priority
low
priority
medium
status
blocked
status
in-progress
status
needs-review
status
ready
type
bug
type
docs
type
enhancement
type
feature
type
refactor
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
customable/claude-mem#267
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Übersicht
Umfassende Überarbeitung der MikroORM Entities nach Best Practices. Aktuell fehlen viele Relationen und moderne MikroORM-Features werden nicht genutzt.
Aktuelle Entity-Struktur (16 Entities)
Kritische Probleme
1. ArchivedOutput → Observation: Nur Field, keine Relation!
Konsequenzen:
2. Session als Hub ohne Relationen
Session sollte der zentrale Hub sein, aber hat keine Relationen:
3. Fehlende ManyToOne Relationen
content_session_id@ManyToOne(() => Session)memory_session_id@ManyToOne(() => Session)content_session_id@ManyToOne(() => Session)memory_session_id@ManyToOne(() => Session)session_id@ManyToOne(() => Session)observation_id@ManyToOne(() => Observation)memory_session_id@ManyToOne(() => Session)memory_session_id@ManyToOne(() => Session)MikroORM Best Practices umsetzen
1. Type-Safe Relations mit
Ref<T>2. Collections mit
orphanRemoval3. Cascade-Optionen
4. Lazy Loading mit
populate5. Unit of Work Pattern (bereits korrekt)
Vorgeschlagene Entity-Struktur
Migration
Schritt 1: Neue Relationen definieren (ohne DB-Änderung)
Schritt 2: Foreign Key Constraints hinzufügen
Schritt 3: SQLite FK Enforcement
Implementierungs-Checkliste
Phase 1: ArchivedOutput Fix (Kritisch)
compressed_observation_idzu@ManyToOneRelation konvertierenPhase 2: Session als Hub
@OneToManyCollections zu Session hinzufügen@ManyToOneSession@ManyToOneSession@ManyToOneSession (2x)@ManyToOneSession + ObservationPhase 3: Type-Safe Relations
Ref<T>Wrapper für alle ManyToOne Relationenref()Helper in ConstructorsPhase 4: Cascade & Orphan Removal
Phase 5: Index-Optimierung
Offene Fragen
Referenzen
Aufwand
Bezug zu #262 (SQLite vs. Alternative Datenbanken)
Bei der Entity-Überarbeitung sollten wir beachten, dass wir laut #262 möglicherweise PostgreSQL als Alternative anbieten wollen.
Database-Agnostische Entities
MikroORM unterstützt beides - wir sollten die Entities so gestalten, dass sie auf beiden DBs funktionieren:
SQLite FK Constraints
SQLite hat standardmäßig keine Foreign Key Enforcement! Das muss explizit aktiviert werden:
Bei PostgreSQL sind FKs automatisch enforced.
MikroORM Features pro Datenbank
Empfehlung
type: 'json'- MikroORM serialisiert automatischpersist: falsefür SQLite, echte FKs für PostgreSQLKonfiguration per Environment
So können wir die MikroORM-Überarbeitung machen und sind für beide Datenbanken vorbereitet.
Phase 1 Complete: ArchivedOutput → Observation Relation
Implemented
compressed_observation_idfrom plain field to proper@ManyToOnerelationRef<Observation>for type-safe lazy loading@OneToMany archivedOutputscollection on Observationref()+getReference()patterncompressed_observation_idcolumnCommit
24215b7- refactor(database): add ManyToOne relation ArchivedOutput → ObservationPhase 2 Challenge: Session String FK Issue
Analysis revealed a schema complexity:
Sessionhas numeric PK (id) but unique stringcontent_session_idUserPrompt.content_session_id→ stringSummary.memory_session_id→ stringClaudeMd.content_session_id/memory_session_id→ stringsRawMessage.session_id→ stringMikroORM relations typically require pointing to the primary key. Options:
@Formula/ virtual propsRecommendation
Phase 2 should be deferred until:
Phase 1 (ArchivedOutput) was the critical fix since it's new code with no legacy data constraints.
All Phases Complete
Phase 1: ArchivedOutput Fix ✅
24215b7- Convertcompressed_observation_idto proper@ManyToOnerelationRef<Observation>for type-safe access@OneToManyon ObservationPhase 2: Session as Hub ✅
cfa8453- Add Session as central hub with OneToMany collectionspersist: false+referencedColumnNamesfor string FK compatibilityPhase 3: Type-Safe Relations ✅
Ref<T>wrapperPhase 4: Cascade & Orphan Removal ✅
232f400- Add cascade options to Observation and SessionPhase 5: Index Optimization ✅
74db210- Add composite indexes for common queries(memory_session_id, created_at_epoch)(session_id, processed),(project, created_at_epoch)Summary
All 5 phases implemented with backwards compatibility preserved. The virtual relation pattern allows ORM navigation without schema changes while keeping existing FK columns for queries.