Sharing the application configuration across plugins
The Loading the plugins’ configurations section discussed how a plugin could access the application configuration. In this case, the plugin accesses the fastify.secret
plain object to access the environment variables.
The configuration may evolve and become more complex. But, if you just intended to centralize the whole plugin’s settings into a dedicated plugin, how could you do that?
We can modify the config.js
plugin and move it to the configs/
directory. By doing this, we are not loading it automatically anymore. Then, we can integrate the @
fastify/mongodb
configuration:
module.exports = fp(async function configLoader (fastify, opts) { Â Â await fastify.register(fastifyEnv, { Â Â Â Â confKey: 'secrets', Â Â Â Â schema: fastify.getSchema('schema:dotenv') Â Â }) Â Â fastify.decorate('config', { Â Â Â Â mongo...