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

fix(core): Fix returning stale data in Role Update Event #3154

Conversation

DanielBiegler
Copy link
Contributor

@DanielBiegler DanielBiegler commented Oct 19, 2024

Description

Previously it returned role which was fetched before(!) applying the update. See definition of role in line 253 and then the event bus publishing on line 281. I see that patchEntity is mutating the existing object instead of cloning which should be fine for the fields, but then the updatedAt is wrong since its not being updated by patchEntity. Event subscribers that depend on the updatedAt timestamp are receiving a wrong updatedAt and or other auto-updated columns.

Also removed the reference returned by pathEntity to make it visually clear that it actually mutates the object instead of returning a new one. This way its unambigious.

Note: Have not look at many other services where this might also be the case.

Question (Edit: solved and reverted)

Edit: Not going through findOne fails the role.e2e-spec.ts tests so I reverted it. Leaving it for history but the following can be ignored now.


I did remove the manual fetch since TypeORM reloads by default. I couldnt come up with an explanation why we are disabling reloading but then refetch manually anyway? I found the commit which disabled the reload 5 years ago but it doesnt make sense to me. The commit says:

By default, a TypeORM .save() operation will also immediately select the saved entity. In the case that the return value is not used, this is redundant and is switched off by passing the { reload: false } option.

Which makes sense, for cases where the return is not used, but in this case we really do want the new value and are refetching it manually via this.findOne(ctx, role.id) . Am I missing something? Maybe something with relations? findOne does add the channels relation in the query but is that relevant? 🤔

Breaking changes

No

Checklist

📌 Always:

  • I have set a clear title
  • My PR is small and contains a single feature
  • I have checked my own PR

👍 Most of the time:

  • I have added or updated test cases
  • I have updated the README if needed

Previously it returned `role` which was fetched before(!) applying the update.
Copy link

vercel bot commented Oct 19, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview Oct 19, 2024 2:09pm

This makes it visually clear that `patchEntity` actually mutates the
object instead of returning a new one.
Copy link

sonarqubecloud bot commented Nov 5, 2024

Copy link
Member

@michaelbromley michaelbromley left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@michaelbromley michaelbromley merged commit 71f85d2 into vendure-ecommerce:master Nov 14, 2024
30 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2024
@DanielBiegler DanielBiegler deleted the fix-role-event-returning-stale-entity branch November 15, 2024 13:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants