moleculerjs / moleculerjs/rfcs

Custom hooks

Open
#5 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Change: Minor Module: Core Stage: 1
Dominant language
No language data
Stars
2
Forks
4
PR merge metrics
No merged PRs in 30d

Description

  • 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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the linked Action Hooks documentation and tracing the ActionHook middleware, then inspect how custom functions are implemented in moleculer-db. The examples identify transform and populate as target functions, but the detailed design and unresolved questions are empty. Done would require an agreed design and implementation scope for custom hooks.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.