Skip to content

inaiat/fastify-di-plugin

Repository files navigation

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
      }
    )
    

About

Simple, unobtrusive depency injection for fastify

Resources

Stars

Watchers

Forks

Packages

No packages published