@fdosruiz/packetjs

Lightweight micro dependency injection container


Keywords
dependency-injection, dependency-injection-container, di, injection, dependency, container, lazy, lazy-loading, javascript, js, node, nodejs, pimple
License
MIT
Install
npm install @fdosruiz/packetjs@1.0.2

Documentation

Packet JS

node install size npm downloads Build status MIT License Gihub package dependents

Packet JS is a micro-dependency injection container for JavaScript/Node applications, written in TypeScript and with lazy loading, instantiating each service on demand with dependency on each other.

Installation

Using npm:

$ npm install @fdosruiz/packetjs

Using yarn:

$ yarn add @fdosruiz/packetjs

Importing

// Using Node.js `require()`
const container = require('@fdosruiz/packetjs');

// Using ES6 imports
import container from '@fdosruiz/packetjs';

Basic usage

Registering a service:

const container = require('@fdosruiz/packetjs'); // Get the container instance.

container.add('Service', () => { /* here the logic */ return new SomeService() });

In some other place:

const container = require('@fdosruiz/packetjs'); // Always get the same instance of the container. (singleton pattern)

const service = container.get('Service'); // Make an instance of the service on demand, with lazy loading.

Adding configuration properties

It is possible to add configuration properties, for service registration and to make the configuration available throughout the application.

const properties = require('./some-configuration-object-in-json-format');
container.addProps(properties);

To get the properties when registering a service:

container.add('Service', ({ props }) => { /* here the logic */ return new SomeService(props.someParam) });

Also, we can get all the properties:

const props = container.getProps();

Factory with multiples dependencies

For configuring the dependency Injection container, the order of registration of each service is indifferent. The callback function receives the container and the properties as an object in the argument: { container, props }:

// Configuring some service
container.add('service', ({ container: c }) => {
    const dao = c.get('dao');
    return new Service(dao);
});

// Configuring a dependency with dao
container.add('dao', ({ container: c }) => {
    const db = c.get('db');
    return new dao(db);
});

// Configuring a database
container.add('db', ({ container: c, props: p }) => {
    return new someDatabase(process.env.USER, process.PASS, p.someConfigurationProp);
});

In another place we can use the container like this:

cosnt something = container.get('service').getSomeThing(id); // Makes an instance of the service with lazy loading for each service.

Instantiating new containers

If you need standalone containers, it is possible to create isolated instances by accessing the Core Container Class:

const { Container } = require('packetjs/lib/core');
const a = new Container();
const b = new Container();
// a !== b

Credits

Packet JS is inspired by pimple, a small PHP dependency injection container.

License

MIT