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.
npm install fastify-di-container --save
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(); // The userRepository is automatically injected
return foundUser;
},
}
}
/* Accoding to the codes above, 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,
}
]
});
// You can use the userService like this.
const userService = app.container.get('userService')
app.listen(3000);
}
startServer();
- 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;
}
}