1 | # Packet JS
|
2 |
|
3 | [![node](https://img.shields.io/node/v/@fdosruiz/packetjs.svg?maxAge=1000)](https://www.npmjs.com/package/@fdosruiz/packetjs)
|
4 | [![install size](https://packagephobia.com/badge?p=@fdosruiz/packetjs)](https://packagephobia.com/result?p=@fdosruiz/packetjs)
|
5 | [![npm downloads](https://img.shields.io/npm/dm/@fdosruiz/packetjs.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@fdosruiz/packetjs)
|
6 | [![Build status](https://github.com/fdosruiz/packetjs/actions/workflows/ci-cd.yml/badge.svg)](https://github.com/fdosruiz/packetjs/actions)
|
7 | [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/fdosruiz/packetjs/blob/main/LICENSE)
|
8 | [![Gihub package dependents](https://badgen.net/github/dependents-pkg/fdosruiz/packetjs)](https://github.com/fdosruiz/packetjs/network/dependents?dependent_type=PACKAGE)
|
9 |
|
10 | 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.
|
11 |
|
12 | ## Installation
|
13 |
|
14 | Using npm:
|
15 |
|
16 | ```bash
|
17 | $ npm install @fdosruiz/packetjs
|
18 | ```
|
19 |
|
20 | Using yarn:
|
21 |
|
22 | ```bash
|
23 | $ yarn add @fdosruiz/packetjs
|
24 | ```
|
25 |
|
26 | ## Importing
|
27 |
|
28 | ```javascript
|
29 | // Using Node.js `require()`
|
30 | const container = require('@fdosruiz/packetjs');
|
31 |
|
32 | // Using ES6 imports
|
33 | import container from '@fdosruiz/packetjs';
|
34 | ```
|
35 |
|
36 | ## Basic usage
|
37 |
|
38 | Registering a service:
|
39 |
|
40 | ```javascript
|
41 | const container = require('@fdosruiz/packetjs'); // Get the container instance.
|
42 |
|
43 | container.add('Service', () => { /* here the logic */ return new SomeService() });
|
44 | ```
|
45 |
|
46 | In some other place:
|
47 |
|
48 | ```javascript
|
49 | const container = require('@fdosruiz/packetjs'); // Always get the same instance of the container. (singleton pattern)
|
50 |
|
51 | const service = container.get('Service'); // Make an instance of the service on demand, with lazy loading.
|
52 | ```
|
53 |
|
54 | ## Adding configuration properties
|
55 |
|
56 | It is possible to add configuration properties, for service registration and to make the configuration available throughout the application.
|
57 |
|
58 | ```javascript
|
59 | const properties = require('./some-configuration-object-in-json-format');
|
60 | container.addProps(properties);
|
61 | ```
|
62 |
|
63 | To get the properties when registering a service:
|
64 |
|
65 | ```javascript
|
66 | container.add('Service', ({ props }) => { /* here the logic */ return new SomeService(props.someParam) });
|
67 | ```
|
68 |
|
69 | Also, we can get all the properties:
|
70 |
|
71 | ```javascript
|
72 | const props = container.getProps();
|
73 | ```
|
74 |
|
75 | ### Factory with multiples dependencies
|
76 |
|
77 | 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 }`:
|
78 |
|
79 | ```javascript
|
80 | // Configuring some service
|
81 | container.add('service', ({ container: c }) => {
|
82 | const dao = c.get('dao');
|
83 | return new Service(dao);
|
84 | });
|
85 |
|
86 | // Configuring a dependency with dao
|
87 | container.add('dao', ({ container: c }) => {
|
88 | const db = c.get('db');
|
89 | return new dao(db);
|
90 | });
|
91 |
|
92 | // Configuring a database
|
93 | container.add('db', ({ container: c, props: p }) => {
|
94 | return new someDatabase(process.env.USER, process.PASS, p.someConfigurationProp);
|
95 | });
|
96 | ```
|
97 |
|
98 | In another place we can use the container like this:
|
99 |
|
100 | ```javascript
|
101 | cosnt something = container.get('service').getSomeThing(id); // Makes an instance of the service with lazy loading for each service.
|
102 | ```
|
103 |
|
104 | ### Instantiating new containers
|
105 |
|
106 | If you need standalone containers, it is possible to create isolated instances by accessing the Core Container Class:
|
107 |
|
108 | ```javascript
|
109 | const { Container } = require('@fdosruiz/packetjs/lib/core');
|
110 | const a = new Container();
|
111 | const b = new Container();
|
112 | // a !== b
|
113 | ```
|
114 |
|
115 | ## Credits
|
116 |
|
117 | Packet JS is inspired by [pimple](https://github.com/silexphp/Pimple), a small PHP dependency injection container.
|
118 |
|
119 | ## License
|
120 |
|
121 | [MIT](https://github.com/fdosruiz/packetjs/blob/main/LICENSE)
|
122 |
|