npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

beaver-di

v1.1.0

Published

Dependency injection container for NodeJS

Downloads

10

Readme

Beaver - Dependency Injection Container for NodeJS

Lean dependency injection container for NodeJS based on parameter naming, which helps instantiating and referencing dependencies.

Installation

You can get the latest release using:

$ npm install beaver-di

or:

$ yarn add beaver-di

Terminology

There are two type of values we are going to hold in our Dependency Injection Container

|Name|Description| |:------|:--------| |factories| functions used to instantiate a dependency, tipically you will use a factory when instantianting a dependency depends on other dependencies| |dependencies | values that are fixed dependencies, typically environment variables or constant values|

Usage

Define a beaver.config.js file where we instantiate the Dependency Injection Container. This will be the only place where coupling occurs

Here you can define either a factory (used to instantiate a dependency) or a plain dependency that doesn't require any other dependency.

// ./beaver.config.js
import userController from './userController';
import userService from './userService';
import database from './database';
import beaver from 'beaver-di';

export default beaver(
  {
    userController,
    userService,
    database,
  }, 
  {
    helloString: 'helloWorld',
    db: {
      connectionString: 'postgres://',
    },
  },
);

Now just require this file at the top level of you application.

Remember that as this is a property naming based dependency injection, meaning that you will need to match your dependency by name on the object passed to beaver. If you need more control over the naming of parameters passed to a factory, you can make use of the connect(factory, dependencies) API

// ./userController.js

// we defined on beaver.config.js userService module on the property userService
export default (userService, helloString) => {
  // this module does not depend on '/userService.js' but it uses it by the dependecy injected argument
  
  const createUser = async (name) => {
    await userService.create({name})
    ...
  };
  
  const salute = () => helloString;
  
  return {
    createUser,
    salute,
  };
};

Examples

You can find a simple example of how to build an Express app using Beaver DI in the examples directory

Walkthrough

We first define the beaver.config.js file

import todo from './api/todo';
import todoController from './api/todo/todo.controller';
import todoService from './api/todo/todo.service';

export default beaver(
  {
    todo,
    todoController,
    todoService,
  },
  {
    database: {
      ...
    },
  },
);

To continue, in our routes module, we get from beaver the instance of todo factory

import beaver from './beaver.config';

export default (app) => {
  const todo = beaver.get('todo');
  app.use('/api/todo', todo);
};

As todo factory dependes on todoController and todoController depends on todoService, all instances are created when executing const todo = beaver.get('todo');

todo module

import { Router } from 'express';

export default (todoController) => {
  const router = Router();

  router.get('/', todoController.getAll);
  router.post('/', todoController.create);
  router.get('/:id', todoController.get);
  router.delete('/:id', todoController.delete);

  return router;
};

todoController module

export default (todoService) => {
  ...
  return {
    getAll,
    get,
    create,
    delete: deleteTodo,
  };
};

todoService module

export default (database) => {
  ...
  return {
    getAll,
    getById,
    create,
    delete: deleteTodo,
  };
};

API

Creating an instance

Core functionality. Used to instantiate Beaver Dependency Injection Container

import beaver from 'beaver-di';

beaver(factories, dependencies)

Connect API

Used to provide flexibility when injecting properties into a factory.

Notes

  • You will need to restructure the parameters in factory to be an object (this object will contain the dependencies).
  • Values in connect method need to be a string to access dependency injected in a object-like structure.
  • When trying to access a dependency which is in a object-like structure, you will always need the connect API (in this example, the connectString parameter.
import { connect } from 'beaver-di';


const factory = ({ service, controller, connectionString }) => {
  ...
};

export default connect(factory, { service: 'userService', controller: 'userController', connectionString: 'databases.postgres.connectionString' });

Instance methods

beaver.get(name)

Retrieve instance of dependency

beaver.factory(name, factory)

Assign dynamically to a specific name a factory function that when executed will create an instance of dependency

beaver.register(name, dependency)

Assign dynamically to a specific name, an instance of a dependency. This can also be used to register constants i.e. beaver.register('TOKEN', 'secretToken!').