@inaiat/fastify-di-plugin
v1.1.1
Published
Simple, unobtrusive depency injection for fastify.
Downloads
323
Maintainers
Readme
fastify-di-plugin
A dependency injection plugin for fastify framework, using awilix
Motivation: I really wanted use fastify-awilix but this plugin make things statics. So, basically, this plugin can be used without problem with parallel tests and so on.
Getting started
yarn add @inaiat/fastify-di-plugin awilix
Next, set up the plugin:
import { fastifyAwilixPlugin } from '@inaiat/fastify-di-plugin'
Next, set up the plugin:
declare module '@inaiat/fastify-di-plugin' {
interface Cradle {
dateService: Date
printDate: string
}
}
const dateService = () => new Date();
const printService = ({dateService: Date}) => dateService().toDateString()
fastify.register(fastifyDiPlugin, {
module: {
dateService: asFunction(dateService).singleton(),
printDate: asFunction(printService).singleton()
}})
server.get(
'/status',
async (request) => {
const cradle = request.diScope.cradle
return cradle.printDate
}
)