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

@rafterjs/di-autoloader

v0.8.75

Published

An autoloading DI container

Downloads

124

Readme

A dependency injection autoloader

Build Status

A config driven autoloader for dependency injection.

Getting started

yarn add @rafterjs/di-autoloader

Example

Check out the files in ./example and ./test/fixtures

The autoloading config

// service-config.js

const servicesConfig = {
    config: {
        path: `${__dirname}/../test/mocks/test-config`,
        dependencies: []
    },
    testClass: {
        path: `${__dirname}/../test/mocks/test-class`,
        dependencies: [
            `config.foo.bar`,
            'testFunction'
        ]
    },
    testFunction: {
        path: `${__dirname}/../test/mocks/test-function`,
        dependencies: []
    }
};

export default servicesConfig;

Example class with DI

// test-class.js

class TestClass {
    // The dependencies will be injected via the constructor
    constructor(bar, testFunction) {
        this._bar = bar;
        this._testFunction = testFunction;
    }

    /**
     * @return {string}
     */
    getBar() {
        return this._bar;
    }

    /**
     * @return {Function}
     */
    getFunction() {
        return this._testFunction;
    }

    /**
     * @return {string}
     */
    getData() {
        return `here's some data`;
    }
}

export default TestClass;

Instantiating the DI container using the autoloader

import BoxDiAutoLoader from '../lib/box-di-autoloader';
import serviceConfig from './service-config';

const boxDiAutoLoader = new BoxDiAutoLoader(serviceConfig);
boxDiAutoLoader.load();

const testClass = boxDiAutoLoader.get('testClass');
console.log(testClass.getData());

Why use dependency injection (DI)?

DI is a well vetted programming pattern that encourages decoupling of services and allows you to maintain separation of concerns. This in turn means your code is more testable and maintainable.

Check out: http://tutorials.jenkov.com/dependency-injection/dependency-injection-benefits.html and https://stackoverflow.com/questions/130794/what-is-dependency-injection

Why should we use it in node?

Just because we have import or require doesn't mean we should forgo programming best practices. What import does is hardcode your dependencies, rather than making them configurable or reusable eg.

Say we have a DbService which currently uses MongoDb. In an import style your code would be something like:

import mongoose from 'mongoose';

class DbService {
    async connect(url) {
        return mongoose.connect(url);
    }

    async find(query, Model) {
        return mongoose.find(query, Model);
    }

    ...
}

But now your app has grown and you realise that the load on your DB is reducing application performance; you need to introduce a caching layer to reduce the load on the DB.

In our import scenario above that would require a bunch of rewriting at the DbService level. Instead if we had structured it like:

class DbService {
    constructor(dao) {
        this._dao = dao;
    }

    async connect(url) {
        return this._dao.connect(url);
    }

    async find(query, Model) {
        return this._dao.find(query, Model);
    }

    ...
}

We could now inject MongoDbCacheDao instead of injecting mongoose. This would handle the caching, and delegate through to Mongo if needed. The DbService doesn't care if it is cached or not; it is none of it's concern. eg

import crypto from 'crypto'; // NOTE: importing from the node standard lib is ok

class MongoDbCacheDao {
    constructor(dao) {
        this._dao = dao;
        this._cache = {};
    }

    async connect(url) {
        return this._dao.connect(url);
    }

    async find(query, Model) {
        const md5sum = crypto.createHash('md5');
        md5sum.update(
            JSON.stringify(query) +
            JSON.stringify(Model)
        );

        const cacheId =  md5sum.digest('hex');
        if (!this._cache[cacheId]) {
            this._cache[cacheId] = await this._dao.find(query, Model);
        }

        return this._cache[cacheId];
    }

    ...
}

So even though we have import/require in node, it is still valuable to separate concerns and use dependency injection. It will allow you to be much more flexible in the future and adapt to changes more quickly.

Why use an autoloader with DI

As your application grows, so too does your dependency graph. Handling all your dependencies manually becomes very tedious and you end up with lots of boilerplate code in the form of factories. Instead, when using an autoloader, you maintain your dependencies in a simple config file and you only need to create factories in rare circumstances when class/function instantiation is much more complex.

Overall, this means less code to write and maintain, which means less complexity and faster time to market.