Packet JS
Packet JS is a lightweight 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
Register a service:
const container = require('@fdosruiz/packetjs');
container.add('Service', () => {
return new SomeService();
});
In some other place:
const container = require('@fdosruiz/packetjs');
// Make the instance of the service on demand, with lazy loading.
const service = container.get('Service');
In both cases,
require('@fdosruiz/packetjs')
always get the same instance of the container.
Adding configuration properties
It is possible to add configuration properties, for service registration or to make the configuration available throughout the application.
const properties = require('./some-configuration-object-in-json-format');
container.addProps(properties);
To get the properties:
const props = container.getProps();
To get the properties when registering a service:
container.add('Service', ({ props }) => {
return new SomeService(props.someParam);
});
To add new properties to existing ones:
container.addProps({
newProperty: { foo: 'bar' }
});
Register multiples services, with dependencies on each other
For configuring the dependency Injection container, the order of registration of each service is indifferent. Each registered service will be called on demand when the main service is called.
On the other hand, 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.env.PASS,
p.someConfigurationProperty
);
});
In another place we can use the container like this:
// Instantiate the services on demand, with lazy loading for each service.
const foo = container.get('service').getBar(id);
Factory
If you need a factory of the registered services, you can use the getFactory
method to always get a different instance of the service:
const service1 = container.getFactory('service');
const service2 = container.getFactory('service');
// service1 !== service2
Instead, with get
method:
const service1 = container.get('service');
const service2 = container.get('service');
// service1 === service2
Instantiating new containers
For standalone containers, it is possible to create isolated instances by accessing the Core Container Class:
const { Container } = require('@fdosruiz/packetjs/lib/core');
const a = new Container();
const b = new Container();
// a !== b
Container API
Method | Arguments | Description |
---|---|---|
add(key, callback) |
- key : Unique key for the new service or function- callback({ container, props }) : Callback function with dependency injection logic.The callback function receives an object with the container and properties in its argument. |
Add a new service or function to the container |
get(key) |
- key : Unique key of the service or function to get |
Always get the same instance for a concrete service |
getFactory(key) |
- key : Unique key of the service or function to get |
Always get a new instance for a concrete service |
addProps(props) |
- props : Configuration properties object (JSON) |
|
getProps() |
Gets the configuration properties object | |
getContainer() |
Always get the same instance of the container. (static method) |
Credits
Packet JS is inspired by pimple, a small PHP dependency injection container.