Char2sGu / mikro-orm-soft-delete

Generic soft delete solution for MikroORM.
Apache License 2.0
31 stars 10 forks source link

Add "recover" option to support recovery after soft deletion #11

Open rahul-rocket opened 4 months ago

rahul-rocket commented 4 months ago

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:

Benefits:

rahul-rocket commented 4 months ago

@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);
Char2sGu commented 4 months ago

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 commented 4 months ago

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

rahul-rocket commented 4 months ago

@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 commented 4 months ago

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.

kallekulp commented 1 month ago

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?

Char2sGu commented 1 month ago

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.