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

Soft delete recursive cascade #8436

Merged
merged 10 commits into from Feb 16, 2022

Conversation

oxeye-yuvalk
Copy link
Contributor

@oxeye-yuvalk oxeye-yuvalk commented Dec 7, 2021

The soft-delete would update recursively the related entities without loading them

related to - #8416

Description of change

The Problem
Today, the cascade soft delete update the related entities just if we would load all the entities that we want to soft-remove
This behavior is bad in term of performance and in addition, don't work recursively
Because there is no feature like that today, a lot of developers write soft delete flow for every entity that includes a lot of code because they need to kill the children as well, and that causes a lot of bugs

The Solution
Change the soft-remove flow to soft-remove all related entities recursively and automatically

  • soft remove the entity
  • get the entity relations and soft remove the inverse entities where the parent id is the first entity id
  • move recursively throw the relations and soft-delete by the parent ids

I prefer to soft remove without loading all the entities and create subject to each of them because it's more efficient

Other option
Add a flag to the soft-remove function that declares that the operation should do the new logic instead of the old one

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change
  • The new commits follow conventions explained in CONTRIBUTING.md

@pleerock
Copy link
Member

I'm really against cascades further complication, but let's merge it for now, later on we need either simplify the codebase, or find a maintainer who is going to deal with constant cascade issues.

@pleerock pleerock merged commit d0f32b3 into typeorm:master Feb 16, 2022
pleerock added a commit that referenced this pull request Feb 17, 2022
pleerock added a commit that referenced this pull request Feb 17, 2022
@pleerock
Copy link
Member

I had to revert these changes because of failing CI. I missed few points in this PR, I'll left comments.

.softDelete()
.from(relation.inverseEntityMetadata.target)
// We get back list of the affected rows primary keys for call again
.returning([primaryPropertyName])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.returning isn't supported by all databases, for example sqlite.

// Move throw all the relation of the subject
for (const relation of subject.metadata.relations) {
// Call recursive function that get the parents primary keys that in used on the inverse side in all one to many relations
await this.executeSoftRemoveRecursive(relation, [Reflect.get(subject.identifier, subject.metadata.primaryColumns[0].propertyName)]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really clear why Reflect.get is used here. Decorators isn't the only source of entity definitions and some people define entities using entity schemas.

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

Successfully merging this pull request may close these issues.

None yet

2 participants