0
votes

I am soft deleting entities by using a "DeletedDate" attribute. These entities are returned from the server so they can be removed from another user's Breeze cache. What is the proper way to detach soft deleted entities?

One approach I have tried is to watch for entity changes and then detach it if it has a DeletedDate:

// Mark soft-deleted entities as detached
manager.entityChanged.subscribe(function (args) {
   if (args && args.entity && args.entity.DeletedDate) {
       args.entity.entityAspect.setDetached();
   }
});

The problem with this approach is that this occurs when relationships are still being built by Breeze, so the detached entity may still be associated with other objects. For example, detachedEntity.Relationship1 = null (correct behavior), but detachedEntity.Relationship2 != null (incorrect behavior) and detachedEntity.Relationship2.RelatedEntities contains detachedEntity (incorrect behavior.)

I am looking for a solution where I can detach these entities properly and remove it from relationships before the results are passed on. Any ideas?


Example:

I modified the Todo Angular sample to demonstrate the issue. I added a Category entity that has a many-to-many relationship with the TodoItem. The entity TodoItemCategory acts as a mapping table between the entities. The DeletedDate property on each entity represents a soft delete.

In the example the "Essential Needs" category is soft deleted. All users will receive this entity in case it is in their cache and needs to be cleaned up. When I detach it during the entityChanged event, some of its relationships are not removed. The correct behavior would be for the relationships to be removed.

How can I detect a soft-deleted entity and have it properly detached, before the query results are passed on?

Download example

1

1 Answers

0
votes

From what I have gathered in the past I believe setDetached is hardly ever the direction you actually want to go.

If I were you the direction I would go is to set the deletedDate property and on success of the save set it to deleted and accept changes client-side something like this -

function deleteMe(me) {
    // Save changes only to the me entity
    manager.saveChanges([me]).then(success);

    function success(data) {
        me.entityAspect.setDeleted();
        me.entityAspect.acceptChanges();
    }
}

This should set the entity as deleted and remove it and it's relationships to other entities and then acceptChanges should detach it from the manager.