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...