UNPKG

4.86 kBMarkdownView Raw
1# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> Portable Abstractions and Patterns for Node.js
2
3This module is a part of the [Pip.Services](http://pip.services.org) polyglot microservices toolkit.
4It provides a set of basic patterns used in microservices or backend services.
5Also the module implemenets a reasonably thin abstraction layer over most fundamental functions across
6all languages supported by the toolkit to facilitate symmetric implementation.
7
8This module contains the following packages:
9- **Commands** - commanding and eventing patterns
10- **Config** - configuration pattern
11- **Convert** - portable value converters
12- **Data** - data patterns
13- **Errors**- application errors
14- **Random** - random data generators
15- **Refer** - locator inversion of control (IoC) pattern
16- **Reflect** - portable reflection utilities
17- **Run** - component life-cycle management patterns
18- **Validate** - validation patterns
19
20<a name="links"></a> Quick links:
21
22* [Configuration Pattern](https://www.pipservices.org/recipies/configuration)
23* [Locator Pattern](https://www.pipservices.org/recipies/references)
24* [Component Lifecycle](https://www.pipservices.org/recipies/component-lifecycle)
25* [Components with Active Logic](https://www.pipservices.org/recipies/active-logic)
26* [Data Patterns](https://www.pipservices.org/recipies/memory-persistence)
27* [API Reference](https://pip-services3-node.github.io/pip-services3-commons-node/globals.html)
28* [Change Log](CHANGELOG.md)
29* [Get Help](https://www.pipservices.org/community/help)
30* [Contribute](https://www.pipservices.org/community/contribute)
31
32## Use
33
34Install the NPM package as
35```bash
36npm install pip-services3-commons-node --save
37```
38
39Then you are ready to start using the Pip.Services patterns to augment your backend code.
40
41For instance, here is how you can implement a component, that receives configuration, get assigned references,
42can be opened and closed using the patterns from this module.
43
44```typescript
45import { IConfigurable } from 'pip-services3-commons-node';
46import { ConfigParams } from 'pip-services3-commons-node';
47import { IReferenceable } from 'pip-services3-commons-node';
48import { IReferences } from 'pip-services3-commons-node';
49import { Descriptor } from 'pip-services3-commons-node';
50import { IOpenable } from 'pip-services3-commons-node';
51
52export class MyComponentA implements IConfigurable, IReferenceable, IOpenable {
53 private _param1: string = "ABC";
54 private _param2: number = 123;
55 private _anotherComponent: MyComponentB;
56 private _opened: boolean = true;
57
58 public configure(config: ConfigParams): void {
59 this._param1 = config.getAsStringWithDefault("param1", this._param1);
60 this._param2 = config.getAsIntegerWithDefault("param2", this._param2);
61 }
62
63 public setReferences(refs: IReferences): void {
64 this._anotherComponent = refs.getOneRequired<MyComponentB>(
65 new Descriptor("myservice", "mycomponent-b", "*", "*", "1.0")
66 );
67 }
68
69 public isOpen(): boolean {
70 return this._opened;
71 }
72
73 public open(correlationId: string, callback: (err: any) => void): void {
74 this._opened = true;
75 console.log("MyComponentA has been opened.");
76 callback(null);
77 }
78
79 public close(correlationId: string, callback: (err: any) => void): void {
80 this._opened = true;
81 console.log("MyComponentA has been closed.");
82 callback(null);
83 }
84
85}
86```
87
88Then here is how the component can be used in the code
89
90```typescript
91import { ConfigParams } from 'pip-services3-commons-node';
92import { References } from 'pip-services3-commons-node';
93import { Descriptor } from 'pip-services3-commons-node';
94
95let myComponentA = new MyComponentA();
96
97// Configure the component
98myComponentA.configure(ConfigParams.fromTuples(
99 'param1', 'XYZ',
100 'param2', 987
101));
102
103// Set references to the component
104myComponentB.setReferences(References.fromTuples(
105 new Descriptor("myservice", "mycomponent-b", "default", "default", "1.0", myComponentB
106));
107
108// Open the component
109myComponentB.open("123", (err) => {
110 console.log("MyComponentA has been opened.");
111 ...
112});
113```
114
115## Develop
116
117For development you shall install the following prerequisites:
118* Node.js 8+
119* Visual Studio Code or another IDE of your choice
120* Docker
121* Typescript
122
123Install dependencies:
124```bash
125npm install
126```
127
128Compile the code:
129```bash
130tsc
131```
132
133Run automated tests:
134```bash
135npm test
136```
137
138Generate API documentation:
139```bash
140./docgen.ps1
141```
142
143Before committing changes run dockerized build and test as:
144```bash
145./build.ps1
146./test.ps1
147./clear.ps1
148```
149
150## Contacts
151
152The module is created and maintained by **Sergey Seroukhov**.
153
154The documentation is written by **Egor Nuzhnykh**, **Alexey Dvoykin**, **Mark Makarychev**.