-
Notifications
You must be signed in to change notification settings - Fork 1
added new option to updateReview using findOneAndUpdate() #4
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
Open
NatachaKey
wants to merge
1
commit into
main
Choose a base branch
from
updateReview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job with the refactor! Now after seeing this and trying it out I'm seeing a few things that make me think you should actually just stick to the previous version after all. First a quick explanation on the difference between the two:
findOneAndUpdatewill do a single database call where it will use the first argument to the function to filter which database rows it needs to update, and then use the second argument to make those updates at once. So it is more efficient and performant; and avoids any race conditions if someone else is making a update to the same database rows at the same time.findOne + savewill do two database calls. One for thefindOnequery, and one for thesavequery. So it's less performant (though in simpler cases like here this probably won't make much of a user-visible difference in the speed of the query. It's more something to think about when working with large databases). As well, you do more fine-grained updates or manipulate the document you get from thefindOnecall with this approach, so this approach is more flexible.In this particular case, one thing I forgot about here is that you want to do two things with the result of the find:
For (1), because
findOneAndUpdatewill try to update based on the filter query; if it doesn't find a result it will just returnnull. So you could do something like the following (and this would allow us to remove the earlierfindOnequery so that we're not still doing 2 queries when we could just do 1):For (2), because the
findOneAndUpdatehappens all in one query, we need to figure out a way to either stop the query from in the middle (not possible) or revert it after it's complete if the user shouldn't have had permission to do the update. In the hooks, we have access to the original document by fetching it from the database in aprehook (but now we're back to doing 2 database queries, one for the originalfindOneAndUpdateand one for thefindOneinside the hook). But the hook has no idea about the requesting user, so we need to get that info back to the controller, where we can do thecheckPermissionscall. Also, in the controller if the permissions check fails, then we need to undo the update:As you can see, this is becoming more complex, which is why I think in this case, the
findOne+saveapproach is probably what we should stick to.If you do choose to stay with the
findOneAndUpdatein this case, then don't forget to make sure that you're updating the product's average rating in afindOneAndUpdatepost-hook instead of asavepost-hook now:Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akosasante thank you for showing different approaches,I definetly will stick to findOne + save one, //need time to figure out what's happening here. I won't merge this PR, just leave it to learn about the options. one question : shoul we always use POST requests while dealing with schemas? it will always be post- with all kings of events in remove/save/update/create... ? -----> ReviewSchema.post('findOneAndUpdate', async function (updatedReview) {....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To clarify, here you're asking about the
preandposthooks in mongoose right? Not aboutPOSTHTTP requests?https://mongoosejs.com/docs/6.x/docs/middleware.html
Both are useful depending on the case.
prehooks are for logic that you want to occur before the query actually happens in the database. The example cases the doc gives are:posthooks are for logic that you want to occur after the query happens in the database but before returning the document/result to the caller. Some example cases are:So it all just depends on your use-case. In many cases you may not need any middleware at all 😄
Please let me know if I misunderstood your question!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akosasante I mixed up all the concepts! In this case we are dealing with the db and mongoose Schema and use it's hooks, i thought that we were making simple http request- my bad!
Thank you so much for such a great, clear explanation with examples, it's clear now😊