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 |
|
3 | This module is a part of the [Pip.Services](http://pip.services.org) polyglot microservices toolkit.
|
4 | It provides a set of basic patterns used in microservices or backend services.
|
5 | Also the module implemenets a reasonably thin abstraction layer over most fundamental functions across
|
6 | all languages supported by the toolkit to facilitate symmetric implementation.
|
7 |
|
8 | This 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 |
|
34 | Install the NPM package as
|
35 | ```bash
|
36 | npm install pip-services3-commons-node --save
|
37 | ```
|
38 |
|
39 | Then you are ready to start using the Pip.Services patterns to augment your backend code.
|
40 |
|
41 | For instance, here is how you can implement a component, that receives configuration, get assigned references,
|
42 | can be opened and closed using the patterns from this module.
|
43 |
|
44 | ```typescript
|
45 | import { IConfigurable } from 'pip-services3-commons-node';
|
46 | import { ConfigParams } from 'pip-services3-commons-node';
|
47 | import { IReferenceable } from 'pip-services3-commons-node';
|
48 | import { IReferences } from 'pip-services3-commons-node';
|
49 | import { Descriptor } from 'pip-services3-commons-node';
|
50 | import { IOpenable } from 'pip-services3-commons-node';
|
51 |
|
52 | export 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 |
|
88 | Then here is how the component can be used in the code
|
89 |
|
90 | ```typescript
|
91 | import { ConfigParams } from 'pip-services3-commons-node';
|
92 | import { References } from 'pip-services3-commons-node';
|
93 | import { Descriptor } from 'pip-services3-commons-node';
|
94 |
|
95 | let myComponentA = new MyComponentA();
|
96 |
|
97 | // Configure the component
|
98 | myComponentA.configure(ConfigParams.fromTuples(
|
99 | 'param1', 'XYZ',
|
100 | 'param2', 987
|
101 | ));
|
102 |
|
103 | // Set references to the component
|
104 | myComponentB.setReferences(References.fromTuples(
|
105 | new Descriptor("myservice", "mycomponent-b", "default", "default", "1.0", myComponentB
|
106 | ));
|
107 |
|
108 | // Open the component
|
109 | myComponentB.open("123", (err) => {
|
110 | console.log("MyComponentA has been opened.");
|
111 | ...
|
112 | });
|
113 | ```
|
114 |
|
115 | ## Develop
|
116 |
|
117 | For 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 |
|
123 | Install dependencies:
|
124 | ```bash
|
125 | npm install
|
126 | ```
|
127 |
|
128 | Compile the code:
|
129 | ```bash
|
130 | tsc
|
131 | ```
|
132 |
|
133 | Run automated tests:
|
134 | ```bash
|
135 | npm test
|
136 | ```
|
137 |
|
138 | Generate API documentation:
|
139 | ```bash
|
140 | ./docgen.ps1
|
141 | ```
|
142 |
|
143 | Before committing changes run dockerized build and test as:
|
144 | ```bash
|
145 | ./build.ps1
|
146 | ./test.ps1
|
147 | ./clear.ps1
|
148 | ```
|
149 |
|
150 | ## Contacts
|
151 |
|
152 | The module is created and maintained by **Sergey Seroukhov**.
|
153 |
|
154 | The documentation is written by **Egor Nuzhnykh**, **Alexey Dvoykin**, **Mark Makarychev**.
|