Add before and after hooks#42
Merged
eduardoboucas merged 2 commits intofeature/hooksfrom Mar 23, 2016
Merged
Conversation
This commit also ensures the hook is called for an array of documents passed to model.create()
|
|
||
| // apply any existing `beforeCreate` hooks | ||
| if (typeof this.settings.hooks.beforeCreate === 'object') { | ||
| if (obj instanceof Array) { |
Contributor
There was a problem hiding this comment.
@eduardoboucas you can see I've had to duplicate the hook apply method here, when we have been passed an array of documents.
The same has been done for the afterCreate hooks.
If you can make this look actually tidy, we'll be all set to merge 👍
Thanks!
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is an addition to #27, with the implementation of before and after hooks, as discussed with @jimlambie. I've pushed this to a separate branch so it's easier to see all the changes and decide if there's anything that needs to be amended before merging back to the
feature/hooksbranch.The current
create,updateanddeletehooks, which are fired before the events take place, were renamed tobeforeCreate,beforeUpdateandbeforeDelete, respectively. The hooksafterCreate,afterUpdateandafterDeleteare now introduced by this PR.Before hooks are always fired, whereas after hooks only fire after and if an operation is successful.
Overview
The following data is passed to each type of hook:
beforeCreate:obj: Fields sent in the requestdata:options: Hook optionsafterCreate:obj: Document created in the databasedata:options: Hook optionsbeforeUpdate:obj: Update query sent in the requestdata:options: Hook optionsupdatedDocs: Documents affected by the updateafterUpdate:obj: Updated documentsdata:options: Hook optionsbeforeDelete:obj: Delete query sent in the requestdata:options: Hook optionsafterDelete:obj: Delete query sent in the requestdata:options: Hook optionsTesting
The following hook may be useful to get a better idea of when exactly each hook type is fired and what data it receives, as it logs to the console its internals every time it gets called:
workspace/hooks/showInfo.js
And then enable it in a model:
workspace/collections/vjoin/testdb/collection.users.json
Let me know your thoughts. (cc @josephdenne @jimlambie)