1

how would you access functions within a controller that are contained within a parent function? In the example below, I am unable to access the getConfig() function.

The reason I have it setup in this manner is so that I can still access the Fastify instance in the controller.

route.js

import formbody from '@fastify/formbody'
import controller from '../../controllers/controller.js'

const routes = (fastify, options, done) => {

   fastify.register(formbody)
   fastify.register(controller)

   fastify.get('/get-config', (req, rep) => {
        getConfig(req, rep)
    })
}

export default routes

controller.js

import merchant from '../models/merchant.js'

const controller = (fastify, options, done) => {

   const srvcMerchant = new merchant(fastify)

   const getConfig = async (req, rep) => {
        const config = await srvcMerchant.getConfig(req.id);

        if (!config) {
            return rep
                .status(404)
                .send({ message: 'Not found' })
        }

        rep.send(config);
    }
}

export default controller
Dally
  • 1,281
  • 4
  • 18
  • 37

1 Answers1

0

You need to write a plugin (without encapsulation) and add a decorator:

controller.js

import merchant from '../models/merchant.js'
import fp from 'fastify-plugin'

const controller = (fastify, options, done) => {

   const srvcMerchant = new merchant(fastify)

   const getConfig = async (req, rep) => {
        const config = await srvcMerchant.getConfig(req.id);

        if (!config) {
            rep.code(404)
            return { message: 'Not found' }
        }

        return config;
    }

    fastify.decorate('foo', getConfig)
    done()
}

export default fp(controller)

then in your route:

fastify.get('/get-config', async (req, rep) => {
    return fastify.foo(req, rep)
})

Additional useful readings:

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
  • Thank you for that Manuel. In your opinion, is this the most efficient way to structure a project. I come from a PHP background where I've always used the MVC framework. I feel like I'm always using fastify-plugin whether it's for controllers, helper functions, middleware, services/models, etc. Can't help but feel I may be over using it and not properly understanding how to leverage Fastify's plugin and encapsulation system. Thank you for sharing those links by the way. – Dally Mar 08 '23 at 09:47
  • This answer requires a book, that I will publish [by august](https://www.packtpub.com/product/accelerating-server-side-development-with-fastify/9781800563582) - I will try to update the answer by collecting some "TLDR" takeaways. eg: if you use the `getConfig` function only in one place, you don't need a decorator – Manuel Spigolon Mar 08 '23 at 10:10
  • I am indeed only using ```getConfig``` in one place. I've been following your book for some time now. Has the release date been pushed back? I was under the assumption it was out this month. Very much looking forward to purchasing your book. I feel there isn't enough information online regarding Fastify which makes it a little difficult when building large projects. – Dally Mar 08 '23 at 10:14