-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Adding current user available to pre/post save hooks #514
Adding current user available to pre/post save hooks #514
Conversation
This is pretty much exactly how I was planning to do this, nice work. I can't remember why I hadn't added it yet, I think I wanted to (either) write tests first, or do some testing to make it really clear in which situations this will and won't work... For example
... which is a bit of a trap, it means if you don't consistently use the I do think it's better to have this even with that limitation, I just wanted to make sure it was well tested and understood :) Also, what do you think of calling it |
This is nice - I can use this on my project. It further sits well with #490 |
@JedWatson, you're absolutely correct. I should have included the caveat that it will not work in circumstances where I see your point about a potential schema conflict. I'll rename it to Let me work on this a bit. I was to include some tests as well. |
@SharadKumar, thanks for the comments. The |
…er-in-pre-post-save Adding current user available to pre/post save hooks
Merged as is to unblock some work, will rename to |
No problem. Will work on some tests tonight and submit a new PR. |
This feature should be in pre/post remove too |
This PR makes the current user available to
pre
andpost
save hooks.I do not alter the model per se. I simply add a
_user
property to theitem
object just before it is saved inupdateHandler
, but sincemongoose
is unaware of it's existence, it does not save it to the database. The property is temporary in nature and does not survive the current transaction.Here's a usage example.
The idea of having the current user available to
pre
andpost
save hooks was originally raised in #458 in the context of using it for audit purposes (which I addressed in #490). However, having the current user available inpre
andpost
save hooks is also quite useful in use cases other than this, which is why I decided to submit this as a separate pull request.