Business Logic Components for Node.js / ES2017
This module is a part of the Pip.Services polyglot microservices toolkit.
The Logic module contains standard component definitions to handle complex business transactions.
The module contains the following packages:
- Cache - distributed cache
- Lock - distributed lock components
- State - distributed state management components
Use
Install the NPM package as
npm install pip-services4-logic-node --save
Example how to use caching and locking. Here we assume that references are passed externally.
import { Descriptor } from 'pip-services4-commons-node';
import { References } from 'pip-services4-commons-node';
import { IReferences } from 'pip-services4-commons-node';
import { IReferenceable } from 'pip-services4-commons-node';
import { ILock } from 'pip-services4-logic-node';
import { MemoryLock } from 'pip-services4-logic-node';
import { ICache } from 'pip-services4-logic-node';
import { MemoryCache } from 'pip-services4-logic-node';
export class MyComponent implements IReferenceable {
private _cache: ICache;
private _lock: ILock;
public setReferences(refs: IReferences): void {
this._cache = refs.getOneRequired<ICache>(new Descriptor("*", "cache", "*", "*", "1.0"));
this._lock = refs.getOneRequired<ILock>(new Descriptor("*", "lock", "*", "*", "1.0"));
}
public async myMethod(context: IContext, param1: any): Promise<any> {
// First check cache for result
result := await this._cache.retrieve(context, "mykey");
if (result != null) {
return result;
}
// Lock..
await this._lock.acquireLock(context, "mykey", 1000, 1000);
// Do processing
...
// Store result to cache async
this._cache.store(context, "mykey", result, 3600000);
// Release lock async
this._lock.releaseLock(context, "mykey");
return result;
}
}
// Use the component
let myComponent = new MyComponent();
myComponent.setReferences(References.fromTuples(
new Descriptor("pip-services", "cache", "memory", "default", "1.0"), new MemoryCache(),
new Descriptor("pip-services", "lock", "memory", "default", "1.0"), new MemoryLock(),
);
result := await myComponent.myMethod(null);
Develop
For development you shall install the following prerequisites:
- Node.js 14+
- Visual Studio Code or another IDE of your choice
- Docker
- Typescript
Install dependencies:
npm install
Compile the code:
tsc
Run automated tests:
npm test
Generate API documentation:
./docgen.ps1
Before committing changes run dockerized build and test as:
./build.ps1
./test.ps1
./clear.ps1
Contacts
The library is created and maintained by Sergey Seroukhov and Danil Prisyazhniy.
The documentation is written by Mark Makarychev and Eugenio Andrieu.