@@ -147,7 +147,7 @@ function updateSomething(action_type, pk, data, transaction, commit) {
147
147
if ( error )
148
148
transaction . rollback ( reject . bind ( this , createError ( 500 , 'cannot read ' + action_type + ' with pk ' + pkToString ( pk ) + ' after creating it' ) ) ) ;
149
149
else {
150
- console . log ( action_type + 'executed successfully. Value ' + JSON . stringify ( value ) ) ;
150
+ console . log ( action_type + ' updated successfully. Value ' + JSON . stringify ( value ) ) ;
151
151
if ( commit && commit === true )
152
152
transaction . commit ( resolve . bind ( this , value ) ) ;
153
153
else
@@ -291,9 +291,54 @@ LevelStorage.prototype.updateGroupPromise = function (group_name, owner, data) {
291
291
292
292
LevelStorage . prototype . deleteGroupPromise = function ( group_name , owner ) {
293
293
console . log ( "arguments for deleteGroupPromise leveldb " + JSON . stringify ( arguments ) ) ;
294
- var pk = buildGroupPk ( group_name , owner ) ;
295
- return deleteSomething ( "groups" , pk , transaction ( this . groups ) , true ) ;
296
294
295
+ function rollback ( t1 , t2 , callback ) {
296
+ t1 . rollback ( t2 . rollback ( callback ) ) ;
297
+ }
298
+ var that = this ;
299
+ var group , entity ;
300
+ return new Promise ( function ( resolve , reject ) {
301
+ var group_pk = buildGroupPk ( group_name , owner ) ;
302
+ var t_groups = transaction ( that . groups ) ;
303
+ var t_entities = transaction ( that . entities ) ;
304
+ readSomething ( "groups" , group_pk , t_groups , false )
305
+ . then ( function ( group ) {
306
+ var entities = group . entities ;
307
+ var readEntities = [ ] ;
308
+ if ( entities && entities . length > 0 ) {
309
+ entities . forEach ( function ( entity_pk ) {
310
+ readEntities . push ( readSomething ( "entities" , entity_pk , t_entities , false ) ) ;
311
+ } ) ;
312
+ return Promise . all ( readEntities ) ;
313
+ } else {
314
+ return Promise . resolve ( [ ] ) ; //return an empty set of entities so that the promise chain keeps going :)
315
+ }
316
+ } ) . then ( function ( entities ) {
317
+ var ps = [ ] ;
318
+ entities . forEach ( function ( e ) {
319
+ ps . push ( new Promise ( function ( re , rej ) {
320
+ e . groups = e . groups . filter ( function ( v ) {
321
+ return ( v . group_name !== group_name || v . owner !== owner ) ;
322
+ } ) ;
323
+ updateSomething ( "entities" , getPk ( "entities" , e ) , e , t_entities , false ) . then ( re , rej ) ;
324
+ } ) ) ;
325
+ } ) ;
326
+ return Promise . all ( ps ) ;
327
+ } ) . then ( function ( res ) {
328
+ console . log ( "finished updating entities to remove group from their attributes" ) ;
329
+ console . log ( "attempting to delete group " + JSON . stringify ( group_pk ) ) ;
330
+ return deleteSomething ( "groups" , group_pk , t_groups , false ) ;
331
+ } ) . then ( function ( ) {
332
+ t_entities . commit ( function ( ) {
333
+ t_groups . commit ( function ( ) {
334
+ resolve ( ) ;
335
+ } ) ;
336
+ } ) ;
337
+ } ) . catch ( function rej ( reason ) {
338
+ console . log ( 'level storage rejecting ' + reason ) ;
339
+ return rollback ( t_entities , t_groups , reject . bind ( this , reason ) ) ;
340
+ } ) ;
341
+ } ) ;
297
342
} ;
298
343
299
344
LevelStorage . prototype . addEntityToGroupPromise = function ( group_name , owner , entity_id , entity_type ) {
0 commit comments