@@ -54,11 +54,18 @@ pub struct Entity {
5454
5555impl < const EVENT_MESSAGE : bool > From < Entity > for torii_proto:: schema:: Entity < EVENT_MESSAGE > {
5656 fn from ( value : Entity ) -> Self {
57- let models = if value. deleted {
58- vec ! [ ]
59- } else {
60- vec ! [ value. updated_model. unwrap( ) . as_struct( ) . unwrap( ) . clone( ) ]
61- } ;
57+ // Always include model info so clause matching works for deletions too.
58+ // The `deleted` flag indicates deletion status - no need to empty models.
59+ let updated_model = value. updated_model . as_ref ( ) . unwrap_or_else ( || {
60+ if value. deleted {
61+ panic ! ( "deleted entity missing updated_model: {}" , value. id) ;
62+ }
63+ panic ! ( "entity missing updated_model: {}" , value. id) ;
64+ } ) ;
65+ let model_struct = updated_model. as_struct ( ) . unwrap_or_else ( || {
66+ panic ! ( "entity updated_model is not a struct: {}" , value. id) ;
67+ } ) ;
68+ let models = vec ! [ model_struct. clone( ) ] ;
6269
6370 // Use the dedicated entity_id column (no parsing needed!)
6471 Self {
@@ -74,6 +81,8 @@ impl<const EVENT_MESSAGE: bool> From<Entity> for torii_proto::schema::Entity<EVE
7481
7582impl < const EVENT_MESSAGE : bool > From < Entity > for EntityWithMetadata < EVENT_MESSAGE > {
7683 fn from ( value : Entity ) -> Self {
84+ let updated_model = value. updated_model . clone ( ) ;
85+ let event_id = value. event_id . clone ( ) ;
7786 let keys = value
7887 . keys
7988 . split ( '/' )
@@ -86,9 +95,10 @@ impl<const EVENT_MESSAGE: bool> From<Entity> for EntityWithMetadata<EVENT_MESSAG
8695 } )
8796 . collect ( ) ;
8897 Self {
89- event_id : value . event_id . clone ( ) ,
98+ event_id,
9099 entity : value. into ( ) ,
91100 keys,
101+ updated_model,
92102 }
93103 }
94104}
@@ -204,6 +214,58 @@ impl From<Event> for torii_proto::Event {
204214 }
205215}
206216
217+ #[ cfg( test) ]
218+ mod tests {
219+ use super :: * ;
220+ use chrono:: Utc ;
221+ use dojo_types:: schema:: { Struct , Ty } ;
222+
223+ fn sample_entity ( updated_model : Option < Ty > , deleted : bool ) -> Entity {
224+ let now = Utc :: now ( ) ;
225+ Entity {
226+ id : "0x1:0x2" . to_string ( ) ,
227+ entity_id : "0x2" . to_string ( ) ,
228+ world_address : "0x1" . to_string ( ) ,
229+ keys : "0x1/0x2" . to_string ( ) ,
230+ event_id : "event_1" . to_string ( ) ,
231+ executed_at : now,
232+ created_at : now,
233+ updated_at : now,
234+ updated_model,
235+ deleted,
236+ }
237+ }
238+
239+ #[ test]
240+ #[ should_panic( expected = "deleted entity missing updated_model" ) ]
241+ fn deleted_entity_requires_updated_model ( ) {
242+ let entity = sample_entity ( None , true ) ;
243+ let _: torii_proto:: schema:: Entity = entity. into ( ) ;
244+ }
245+
246+ #[ test]
247+ fn deleted_entity_includes_model_info ( ) {
248+ let model = Struct {
249+ name : "ns-Model" . to_string ( ) ,
250+ children : vec ! [ ] ,
251+ } ;
252+ let entity = sample_entity ( Some ( Ty :: Struct ( model. clone ( ) ) ) , true ) ;
253+
254+ let proto_entity: torii_proto:: schema:: Entity = entity. clone ( ) . into ( ) ;
255+ assert_eq ! ( proto_entity. models. len( ) , 1 ) ;
256+ assert_eq ! ( proto_entity. models[ 0 ] . name, model. name) ;
257+
258+ let entity_with_metadata: EntityWithMetadata = entity. into ( ) ;
259+ let updated_model = entity_with_metadata
260+ . updated_model
261+ . expect ( "updated_model missing" ) ;
262+ match updated_model {
263+ Ty :: Struct ( struct_ty) => assert_eq ! ( struct_ty. name, "ns-Model" ) ,
264+ _ => panic ! ( "expected updated_model to be a struct" ) ,
265+ }
266+ }
267+ }
268+
207269#[ derive( FromRow , Deserialize , Debug , Clone ) ]
208270#[ serde( rename_all = "camelCase" ) ]
209271pub struct Token {
0 commit comments