Skip to content

Add before and after hooks#42

Merged
eduardoboucas merged 2 commits intofeature/hooksfrom
feature/hooks-v2
Mar 23, 2016
Merged

Add before and after hooks#42
eduardoboucas merged 2 commits intofeature/hooksfrom
feature/hooks-v2

Conversation

@eduardoboucas
Copy link
Contributor

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/hooks branch.

The current create, update and delete hooks, which are fired before the events take place, were renamed to beforeCreate, beforeUpdate and beforeDelete, respectively. The hooks afterCreate, afterUpdate and afterDelete are 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 request
    • data:
      • options: Hook options
  • afterCreate:
    • obj: Document created in the database
    • data:
      • options: Hook options
  • beforeUpdate:
    • obj: Update query sent in the request
    • data:
      • options: Hook options
      • updatedDocs: Documents affected by the update
  • afterUpdate:
    • obj: Updated documents
    • data:
      • options: Hook options
  • beforeDelete:
    • obj: Delete query sent in the request
    • data:
      • options: Hook options
  • afterDelete:
    • obj: Delete query sent in the request
    • data:
      • options: Hook options

Testing

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

module.exports = function (obj, type, data) {
  console.log('');
  console.log('**** HOOK ***');
  console.log('Type:', type);
  console.log('Payload:', obj);
  console.log('Additional data:', data);
  console.log('');

  return obj;
};

And then enable it in a model:

workspace/collections/vjoin/testdb/collection.users.json

"hooks": {
  "beforeCreate": ["showInfo"],
  "afterCreate": ["showInfo"],
  "beforeUpdate": ["showInfo"],
  "afterUpdate": ["showInfo"],
  "beforeDelete": ["showInfo"],
  "afterDelete": ["showInfo"]
}

Let me know your thoughts. (cc @josephdenne @jimlambie)

Eduardo Boucas and others added 2 commits March 20, 2016 13:21
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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@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!

@jimlambie jimlambie mentioned this pull request Mar 23, 2016
@eduardoboucas eduardoboucas merged commit f3bfb6c into feature/hooks Mar 23, 2016
@eduardoboucas eduardoboucas deleted the feature/hooks-v2 branch March 23, 2016 09:45
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.

2 participants