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

Custom hooks #5

Open
icebob opened this issue Mar 8, 2020 · 0 comments
Open

Custom hooks #5

icebob opened this issue Mar 8, 2020 · 0 comments

Comments

@icebob
Copy link
Member

icebob commented Mar 8, 2020

  • Start Date: 2020-03-08
  • Target Version: 0.14
  • Reference Issues: -
  • Implementation PR: -

Summary

We should extend the current Action Hooks feature. It is used by the ActionHook middleware and it can be defined only before, after & error hooks for actions.

For some major components, it would be good if the user can define custom hooks for the custom functions, e.g. define a beforeTransform or afterPopulate hook in moleculer-db.

Basic example

module.exports = {
    name: "posts",
    mixins: [DbService],
    hooks: {
        // It will be called before transforming
        beforeTransform(ctx, params, doc) {
            doc.id = encrypt(doc._id);
            delete doc._id;
        },

        // It will be called after populating
        afterPopulate(ctx, populate, doc) {
            if (doc.author) {
                delete doc.authorID;
            }
        }
    },
};
function transformDocuments(ctx, params, doc) {
  // Call beforeTransform hook
  if (this.hooks.beforeTransform) {
    await this.hooks.beforeTransform(ctx, params, doc);
  }

  //  Do transforming

  // Call afterTransform hook
  if (this.hooks.afterTransform) {
    await this.hooks.afterTransform(ctx, params, doc);
  }
}

Motivation

Detailed design

Drawbacks

Alternatives

Adoption strategy

Unresolved questions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant