@@ -266,8 +266,55 @@ LevelStorage.prototype.updateEntityPromise = function (entity_id, entity_type, d
266266//deletes the entity with the given id and all its attributes 
267267LevelStorage . prototype . deleteEntityPromise  =  function  ( entity_id ,  entity_type )  { 
268268  console . log ( "arguments for deleteEntity leveldb "  +  JSON . stringify ( arguments ) ) ; 
269-   var  pk  =  buildEntityPk ( entity_id ,  entity_type ) ; 
270-   return  deleteSomething ( "entities" ,  pk ,  transaction ( this . entities ) ,  true ) ; 
269+ 
270+   function  rollback ( t1 ,  t2 ,  callback )  { 
271+     t1 . rollback ( t2 . rollback ( callback ) ) ; 
272+   } 
273+   var  that  =  this ; 
274+   var  group ,  entity ; 
275+   return  new  Promise ( function  ( resolve ,  reject )  { 
276+     var  pk  =  buildEntityPk ( entity_id ,  entity_type ) ; 
277+     var  t_groups  =  transaction ( that . groups ) ; 
278+     var  t_entities  =  transaction ( that . entities ) ; 
279+     readSomething ( "entity" ,  pk ,  t_entities ,  false ) 
280+       . then ( function  ( entity )  { 
281+         var  groups  =  entity . groups ; 
282+         var  readGroups  =  [ ] ; 
283+         if  ( groups  &&  groups . length  >  0 )  { 
284+           groups . forEach ( function  ( group_pk )  { 
285+             readGroups . push ( readSomething ( "groups" ,  group_pk ,  t_groups ,  false ) ) ; 
286+           } ) ; 
287+           return  Promise . all ( readGroups ) ; 
288+         }  else  { 
289+           return  Promise . resolve ( [ ] ) ;  //return an empty set of entities so that the promise chain keeps going :) 
290+         } 
291+       } ) . then ( function  ( groups )  { 
292+         var  ps  =  [ ] ; 
293+         groups . forEach ( function  ( g )  { 
294+           ps . push ( new  Promise ( function  ( re ,  rej )  { 
295+             g . entities  =  g . entities . filter ( function  ( v )  { 
296+               return  ( v . type  !==  entity_type  ||  v . id  !==  entity_id ) ; 
297+             } ) ; 
298+             updateSomething ( "groups" ,  buildGroupPk ( g . group_name ,  g . owner ) ,  g ,  t_groups ,  false ) . then ( re ,  rej ) ; 
299+           } ) ) ; 
300+         } ) ; 
301+         return  Promise . all ( ps ) ; 
302+       } ) . then ( function  ( res )  { 
303+         console . log ( "finished updating groups by removing entity from their attributes" ) ; 
304+         console . log ( "attempting to delete entity "  +  JSON . stringify ( pk ) ) ; 
305+         return  deleteSomething ( "entities" ,  pk ,  t_entities ,  false ) ; 
306+       } ) . then ( function  ( )  { 
307+         t_entities . commit ( function  ( )  { 
308+           t_groups . commit ( function  ( )  { 
309+             resolve ( ) ; 
310+           } ) ; 
311+         } ) ; 
312+       } ) . catch ( function  rej ( reason )  { 
313+         console . log ( 'level storage rejecting '  +  reason ) ; 
314+         return  rollback ( t_entities ,  t_groups ,  reject . bind ( this ,  reason ) ) ; 
315+       } ) ; 
316+   } ) ; 
317+ 
271318} ; 
272319
273320LevelStorage . prototype . createGroupPromise  =  function  ( group_name ,  owner )  { 
0 commit comments