# pip-services3-container-node

> IoC container for Pip.Services in Node.js

Latest version **3.0.7** (published 2021-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install pip-services3-container-node
pnpm add pip-services3-container-node
yarn add pip-services3-container-node
bun add pip-services3-container-node
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.7 |
| Published | 2021-04-21 |
| First published | 2018-11-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=0.6.14 |
| Dependencies | 2 |
| Unpacked size | 195 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Conceptual Vision Consulting LLC |
| Maintainers | seroukhov |
| Keywords | pip.services, microservice, container |

## Links

- npm: https://www.npmjs.com/package/pip-services3-container-node
- Repository: https://github.com/pip-services-node/pip-services-container-node
- Homepage: https://github.com/pip-services-node/pip-services-container-node#readme
- Issues: https://github.com/pip-services-node/pip-services-container-node/issues
- npm.io page: https://npm.io/package/pip-services3-container-node

## Dependencies (2)

- [pip-services3-commons-node](https://npm.io/package/pip-services3-commons-node.md) ^3.0.4
- [pip-services3-components-node](https://npm.io/package/pip-services3-components-node.md) ^3.0.6

## Recent versions

- 3.0.7 (latest) — 2021-04-21
- 3.0.5 — 2020-11-29
- 3.0.4 — 2018-11-03

## README

# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> IoC container for Node.js

This module is a part of the [Pip.Services](http://pipservices.org) polyglot microservices toolkit. It provides an inversion-of-control (IoC) container to facilitate the development of services and applications composed of loosely coupled components.

The module containes a basic in-memory container that can be embedded inside a service or application, or can be run by itself.
The second container type can run as a system level process and can be configured via command line arguments.
Also it can be used to create docker containers.

The containers can read configuration from JSON or YAML files use it as a recipe for instantiating and configuring components.
Component factories are used to create components based on their locators (descriptor) defined in the container configuration.
The factories shall be registered in containers or dynamically in the container configuration file.

The module contains the following packages:
- **Core** - Basic in-memory and process containers
- **Build** - Default container factory
- **Config** - Container configuration components
- **Refer** - Inter-container reference management (implementation of the Referenceable pattern inside an IoC container)

<a name="links"></a> Quick links:
* [API Reference](https://pip-services3-node.github.io/pip-services3-container-node/globals.html)
* [Change Log](CHANGELOG.md)
* [Get Help](https://www.pipservices.org/community/help)
* [Contribute](https://www.pipservices.org/community/contribute)

## Use

Install the NPM package as
```bash
npm install pip-services3-container-node --save
```

Create a factory to create components based on their locators (descriptors).

```typescript
import { Factory } from 'pip-services3-components-node';
import { Descriptor } from 'pip-services3-commons-node';

export class MyFactory extends Factory {
  public static MyComponentDescriptor = new Descritor("myservice", "mycomponent", "default", "*", "1.0");

  public constructor() {
    super();
    
    this.registerAsType(MyFactor.MyComponentDescriptor, MyComponent);
  }
}
```

Then create a process container and register the factory there. You can also register factories defined in other
modules if you plan to include external components into your container.

```typescript
import { ProcessContainer } from 'pip-services3-container-node';
import { DefaultRpcFactory } from 'pip-services3-rpc-node'; 

export class MyProcess extends ProcessContainer {
  public constructor() {
    super('myservice', 'My service running as a process');
    
    this._factories.add(new DefaultRpcFactory());
    this._factories.add(new MyFactory());
  }
}
```

Define YAML configuration file with components and their descriptors.
The configuration file is pre-processed using [Handlebars templating engine](https://handlebarsjs.com)
that allows to inject configuration parameters or dynamically include/exclude components using conditional blocks.
The values for the templating engine are defined via process command line arguments or via environment variables.
Support for environment variables works well in docker or other containers like AWS Lambda functions.

```yaml
---
# Context information
- descriptor: "pip-services:context-info:default:default:1.0"
  name: myservice
  description: My service running in a process container

# Console logger
- descriptor: "pip-services:logger:console:default:1.0"
  level: {{LOG_LEVEL}}{{^LOG_LEVEL}}info{{/LOG_LEVEL}}

# Performance counters that posts values to log
- descriptor: "pip-services:counters:log:default:1.0"
  
# My component
- descriptor: "myservice:mycomponent:default:default:1.0"
  param1: XYZ
  param2: 987
  
{{#if HTTP_ENABLED}}
# HTTP endpoint version 1.0
- descriptor: "pip-services:endpoint:http:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: {{HTTP_PORT}}{{^HTTP_PORT}}8080{{/HTTP_PORT}}

 # Default Status
- descriptor: "pip-services:status-service:http:default:1.0"

# Default Heartbeat
- descriptor: "pip-services:heartbeat-service:http:default:1.0"
{{/if}}
```

To instantiate and run the container we need a simple process launcher.

```javascript
let MyProcess = require('./MyProcess').MyProcess;

try {
    let proc = new MyProcess();
    proc._configPath = "./config/config.yml";
    proc.run(process.argv);
} catch (ex) {
    console.error(ex);
}
```

And, finally, you can run your service launcher as
```bash
node ./service.js
```

## Develop

For development you shall install the following prerequisites:
* Node.js 8+
* Visual Studio Code or another IDE of your choice
* Docker
* Typescript

Install dependencies:
```bash
npm install
```

Compile the code:
```bash
tsc
```

Run automated tests:
```bash
npm test
```

Generate API documentation:
```bash
./docgen.ps1
```

Before committing changes run dockerized build and test as:
```bash
./build.ps1
./test.ps1
./clear.ps1
```

## Contacts

The Node.js version of Pip.Services is created and maintained by:
- **Volodymyr Tkachenko**
- **Sergey Seroukhov**
- **Mark Zontak**

The documentation is written by:
- **Mark Makarychev**

---
_Source: https://npm.io/package/pip-services3-container-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
