-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Understand how to use Fastify plugin #1056
Comments
What do you mean exactly with an inner service? When decorating a Then, you can do things like: fastifyInstance.decorate('service', function (..args) {
await this.otherDecorator();
// do your thing
}) So the reference to the main |
my flow is Route -> Controller -> Service, the Controller look like this
I can easily access the Redis via server, now I want to modify the Controller into this
In Express, I only need to import the Redis client. |
I'd say that it is mostly up to you; you can also import and reference it there directly, or follow a Dependency Injection pattern. fastify.decorate('cacheOrderDetail', cacheOrderDetail(fastify.redis));
// later in code
fastify.cacheOrderDetail(order); It will depend on what methodology, or principles are you following within your application structure |
Still I do not understand why I need plugin if I can import the client directly in code |
We are not restricting developer to use the plugin, you can use the controller just like normal import. The benefit of plugin is that,
I knows that you can achieve it with many different way. |
Thank you, I'm looking into DI to achieve my purpose. |
What are you trying to achieve, or the steps to reproduce?
I'm studying Fastify and register plugin with @fastify/redis like this
And register route:
and inside v1 route
route with controller
with getOrders function, I can access Redis like this:
But what if I want to access Redis from an inner service, how can I access Redis from the
FastifyInstance, like
Should I init a client before passing it to the plugin and reuse it wherever does not have access to the FastifyInstance?
import redis from "./cache/redis.js"
I searched on Google but still can not find any satisfactory answer.
Many thanks
Context
The text was updated successfully, but these errors were encountered: