Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rahul-rocket opened this issue Apr 25, 2024 · 7 comments
Open

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

rahul-rocket opened this issue Apr 25, 2024 · 7 comments

Comments

@rahul-rocket
Copy link

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
Copy link
Author

rahul-rocket commented Apr 25, 2024

@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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

@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
Copy link
Author

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
Copy link

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants