New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fastify-di-container

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-di-container

This plugin is the small, convenient dependency container.

1.0.10
latest
Source
npm
Version published
Maintainers
1
Created
Source

fastify-di-container

This plugin is the small, convenient dependency container.

You can save the components you want to manage as a global singleton into the container. To get and use those components, you only need to set the parameter name to the name of the saved component.

github npm package npm package

Installation

npm install fastify-di-container --save

Example

with ESM syntax:

import Fastify from 'fastify';
import container from 'fastify-di-container';

// in user.repository.ts
function makeUserRepository() {
  return {
    findOneFromDB: async (userId) => (
      // ... return from db matched with userId
    ),
  }
}

// in user.service.ts
function makeUserService(userRepository/*Parameter name matched with component name*/) {
  return {
    getUserById: async () => {
      const foundUser = await userRepository.findOneFromDB();
      return foundUser;
    },
  }
}

/* Now, UserService component has dependency to userRepository */

async function startServer() {
  const app = Fastify();
  // Just register each module as component
  await app.register(container, {
    components: [
      // Describe 'Component Summaries' in this.
      {
        name: 'userService',
        constructor: makeUserService,
      },
      {
        name: 'userRepository',
        constructor: makeUserRepository,
      }
    ]
  });

  app.listen(3000);
}

startServer();

Additional options example

  • Customize container name
  • onInitializedHook

with Typescript (It works well on javascript too)

import container from 'fastify-di-container';
import fastify from 'fastify';

const app = fastify();

app.register(container, {
  components: [
    /*... Component Summaries to register*/
  ],
  containerName: 'customContainer',
  onInitialized: <UserService>(componentName, initializedComponent) => {
    console.log(componentName);
  },
});

const userService = app.customContainer.get<UserService>('userService');

app.listen(3000);

// Only for typescript.
// If you don't use typescript, skip this.
// in type.d.ts
import { IncommingMessage, Server, ServerResponse } from 'http';
import type { Container } from 'fastify-di-container';
declare module 'fastify' {
  export interface FastifyInstance<
    HttpServer = Server,
    HttpRequest = IncommingMessage,
    HttpResponse = ServerResponse
  > {
    customContainer: Container;
  }
}

Keywords

fastify

FAQs

Package last updated on 04 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts