Eomm / fastify-overview

Build a structure graph of your Fastify application
MIT License
64 stars 6 forks source link

feat: Provide a plugin's graph #110

Open metcoder95 opened 3 months ago

metcoder95 commented 3 months ago

Prerequisites

🚀 Feature Proposal

Have the opportunity of visualize the plugins of a fastify application in a form of a graph to understand relationships and dependencies between plugins.

Motivation

From: https://github.com/fastify/help/issues/1018

Is there any way to specify which onClose (or preClose) hooks from each plugin should be executed before the others? I have some plugins that rely on other main plugins in order to execute correctly their onClose hooks.

E.G.: Some plugins need the database plugin in order to perform some cleaning operations on the database before closing the server. If the database plugin gets torn down before the plugin, the database client results disconnected.

Transferred from: https://github.com/fastify/fastify/issues/5375

Example

const fastify = require('fastify');
const dbPlugin = require('./my-db-plugin');
const otherPlugin = require('./my-other-plugin');

// my-db-plugin.js
async function plugin(instance, opts) {
    fastify.decorate('database', mydbdriver);

    fastify.addHook('onClose', async (app) => {
        console.log('second');
        // Let's say that my-other-plugin requires
        // the database plugin to execute some cleanup work, whatever this could be
        await app.database.close();
    });
}

module.exports = fp(plugin, { name: ['database']});

// my-other-plugin.js
async function plugin(instance, opts) {
    fastify.addHook('onClose', async (app) => {
        console.log('first');
        // Let's say that my-other-plugin requires
        // the database plugin to execute some cleanup work, whatever this could be
        await app.database.cleanup();
    });
}

module.exports = fp(plugin, { name: 'other', dependencies: ['database']});

// index.js
// ... all the bootup process
await fastify.ready();

console.log(fastify.overview());

/** Just an illustrative example
{
  "plugins": {
    "my-other-plugin": {
      "depends": {
        "my-db-plugin": {
          "depends": {}
        }
      }
    }
  }
}
*/