# OSIRIS Type Registry Design ## Problem We need applications (like ZDFZ API) to register custom types with OSIRIS so that: 1. The `save()` method can use the correct struct type instead of hardcoding `Note` 2. Each collection name maps to a specific Rust type 3. The type system properly deserializes, indexes, and stores data ## Challenge The `Object` trait is not "dyn compatible" (object-safe) because it has: - Associated functions (`object_type()`, `from_json()`) - Generic methods - Serialize/Deserialize bounds This means we **cannot** use `Box` for dynamic dispatch. ## Solution: Type Registry with Callbacks Instead of trying to return `Box`, we use a callback-based approach: ```rust pub struct TypeRegistry { // For each collection, store a function that: // 1. Takes JSON string // 2. Deserializes to the correct type // 3. Stores it using GenericStore // 4. Returns the ID savers: HashMap Result<()>>>, } ``` ### Usage in ZDFZ API: ```rust // Create registry let registry = TypeRegistry::new(); // Register Resident type registry.register_saver("residents", |store, id, json| { let mut resident: Resident = serde_json::from_str(json)?; resident.set_id(id); store.put(&resident).await }); // Register Company type registry.register_saver("companies", |store, id, json| { let mut company: Company = serde_json::from_str(json)?; company.set_id(id); store.put(&company).await }); // Create OSIRIS context with registry let ctx = OsirisContext::new_with_registry( "my_context", "owner_id", herodb_url, db_id, Some(Arc::new(registry)) ); // Now save() uses the registered type! ctx.save("residents", "id123", resident_json)?; ``` ### Benefits: ✅ **Type-safe** - Each collection uses its proper Rust type ✅ **Flexible** - Applications register their own types ✅ **No trait object issues** - Uses closures instead of `Box` ✅ **Proper indexing** - Each type's `index_keys()` method is called ✅ **Clean API** - Simple registration interface ## Implementation Plan: 1. ✅ Create `TypeRegistry` with callback-based savers 2. ✅ Add `set_registry()` to `GenericStore` 3. ✅ Update `OsirisContext::save()` to use registry if available 4. ✅ Fall back to `Note` if no registry or collection not registered 5. Document usage for ZDFZ API ## Next Steps: The type registry infrastructure is in place. Now ZDFZ API can: 1. Create a `TypeRegistry` 2. Register all SDK model types 3. Pass registry when creating OSIRIS contexts 4. Use generic `save()` method with proper types! --- **Status:** Design complete, ready for implementation with callback approach.