mikro-orm-soft-delete icon indicating copy to clipboard operation
mikro-orm-soft-delete copied to clipboard

Add "recover" option to support recovery after soft deletion

Open rahul-rocket opened this issue 1 year ago • 7 comments

Description: It would be helpful to have an option to recover entities that have been soft-deleted. This feature would enable users to restore entities to their original state, rather than permanently losing them due to soft deletion.

Feature Request:

  • Implement a method to recover soft-deleted entities.
  • Ensure that the method updates the appropriate flags/fields to mark the entity as active.
  • Include documentation and examples for using this feature.

Benefits:

  • Provides users with the ability to recover entities that may have been accidentally soft-deleted.
  • Increases flexibility in managing soft-deleted data.
  • Reduces risk of permanent data loss.

rahul-rocket avatar Apr 25 '24 09:04 rahul-rocket

@Char2sGu Okay. So below code is working to recover it, but still not sure how to recover deleted relationships.

// Reset the soft-delete flag to "recover" the entity
wrap(entity as BaseEntity).assign({ deletedAt: null });

// Persist all changes to ensure recovery is complete
await this.mikroOrmRepository.persistAndFlush(entity);

rahul-rocket avatar Apr 25 '24 10:04 rahul-rocket

Hey @rahul-rocket sorry for the delay. I am a little busy at the moment, but this has been added to my todo. Will look into it in a few days

Char2sGu avatar Apr 30 '24 17:04 Char2sGu

On second thought, I should stop piling up my to-do list 😝

Currently, when an entity is soft-deleted, only this specific entity's deletedAt property will be updated. The relations will not be updated (but the relations will not appear in queries because of the filter). Therefore, if I didn't confuse myself, simply query the deleted entity with the filters disabled, and set deletedAt back to its initial value should recover the entity.

I'm not pretty sure if this should be added as a new method since this process seems pretty straightforward. Do you have some suggestions on the API you expect to see? @rahul-rocket

Char2sGu avatar Apr 30 '24 17:04 Char2sGu

@Char2sGu We are building amazing platform where we used 2 ORMs (TypeORM & MikroORM) together. So TypeORM provide additional method to recover it.

i.e. recover<T extends DeepPartial<Entity>>(entity: T, options?: SaveOptions): Promise<T & Entity>;

So TypeORM can also soft deleted relations too if we passed them with Entity. But not sure how Mikro ORM (this package) doing it.

Note: Anyway i have fixed this issue by some other hacked. But check below link and let me know if you have any suggestion for me.

https://github.com/ever-co/ever-gauzy/blob/develop/packages/core/src/core/crud/crud.service.ts#L684

rahul-rocket avatar May 01 '24 05:05 rahul-rocket

On second thought, I should stop piling up my to-do list 😝

Currently, when an entity is soft-deleted, only this specific entity's deletedAt property will be updated. The relations will not be updated (but the relations will not appear in queries because of the filter). Therefore, if I didn't confuse myself, simply query the deleted entity with the filters disabled, and set deletedAt back to its initial value should recover the entity.

I'm not pretty sure if this should be added as a new method since this process seems pretty straightforward. Do you have some suggestions on the API you expect to see? @rahul-rocket

My suggestion is that we should add some additional method Recover to entity same as TypeORM if it is possible.

rahul-rocket avatar May 01 '24 05:05 rahul-rocket

On second thought, I should stop piling up my to-do list 😝

Currently, when an entity is soft-deleted, only this specific entity's deletedAt property will be updated. The relations will not be updated (but the relations will not appear in queries because of the filter). Therefore, if I didn't confuse myself, simply query the deleted entity with the filters disabled, and set deletedAt back to its initial value should recover the entity.

I'm not pretty sure if this should be added as a new method since this process seems pretty straightforward. Do you have some suggestions on the API you expect to see? @rahul-rocket

this is not true to be honest. at least for me it deletes the loaded @OneToMany relationships for example. is this expected?

kallekulp avatar Jul 25 '24 06:07 kallekulp

this is not true to be honest. at least for me it deletes the loaded @OneToMany relationships for example. is this expected?

This is not expected. MikroORM might have changed its internal strategies.
Ur always welcome to create PRs for tests and updates.

Char2sGu avatar Jul 25 '24 09:07 Char2sGu