knifecycle
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -0,1 +1,6 @@ | ||
<a name="1.3.1"></a> | ||
## [1.3.1](https://github.com/nfroidure/knifecycle/compare/v1.3.0...v1.3.1) (2017-03-14) | ||
<a name="1.3.0"></a> | ||
@@ -2,0 +7,0 @@ # [1.3.0](https://github.com/nfroidure/knifecycle/compare/v1.2.0...v1.3.0) (2017-03-08) |
{ | ||
"name": "knifecycle", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Manage your NodeJS processes's lifecycle.", | ||
@@ -34,3 +34,3 @@ "main": "dist/index.js", | ||
"metapak": "metapak || echo 'Please `npm install --save-dev metapak`' && exit 0", | ||
"postinstall": "npm run metapak; npm run metapak --silent", | ||
"postinstall": "npm run metapak --silent", | ||
"prepublish": "npm run compile", | ||
@@ -37,0 +37,0 @@ "preversion": "npm t && npm run lint && npm run compile", |
110
README.md
@@ -35,17 +35,20 @@ <!-- | ||
- services management: start services taking their dependencies in count and | ||
shut them down the same way for graceful exits. | ||
shut them down the same way for graceful exits (namely dependency injection | ||
with inverted control); | ||
- easy end to end testing: just replace your services per your own mocks and | ||
stubs while ensuring your application integrity between testing and production. | ||
- isolation: isolate processing in a clean manner, per concerns. | ||
stubs while ensuring your application integrity between testing and production; | ||
- isolation: isolate processing in a clean manner, per concerns; | ||
- functional programming ready: encapsulate global states allowing the rest of | ||
your application to be purely functional. | ||
your application to be purely functional; | ||
- no circular dependencies for services: while circular dependencies are not a | ||
problem within purely functional libraries (require allows it), it may be | ||
harmful for your services, knifecycle impeach that while providing an `$inject` | ||
service à la Angular to allow accessing existing services references. | ||
harmful for your services, `knifecycle` impeach that while providing an `$inject` | ||
service à la Angular to allow accessing existing services references if you | ||
really need to; | ||
- generate Mermaid graphs of the dependency tree. | ||
## Usage | ||
Using Knifecycle is all about declaring the services our application need. Some | ||
of them are simple constants: | ||
Using Knifecycle is all about declaring the services our application needs. | ||
Some of them are simple constants: | ||
```js | ||
@@ -108,6 +111,15 @@ // services/core.js | ||
// services/db.js | ||
import { depends, provider } from 'knifecycle/instance'; | ||
import { depends, provider, constant } from 'knifecycle/instance'; | ||
import MongoClient from 'mongodb'; | ||
constant('DB_CONFIG', { uri: 'mongo:xxxxx' }); | ||
// Register a service with the provider method. | ||
provider('db', | ||
// Declare the service dependencies with the depends decorator | ||
depends(['DB_CONFIG', 'logger'], | ||
dbProvider | ||
) | ||
); | ||
// A service provider returns a service descriptor promise exposing: | ||
@@ -117,22 +129,30 @@ // - a mandatory service property containing the actual service | ||
// - an optional error promise to handle the service failure | ||
provider('db', | ||
// Declare the service dependencies with the depends decorator | ||
depends(['ENV', 'logger'], | ||
function dbProvider({ ENV, logger }) { | ||
return MongoClient.connect(ENV.DB_URI) | ||
.then(function(db) { | ||
let fatalErrorPromise = new Promise((resolve, reject) { | ||
db.once('error', reject); | ||
}); | ||
function dbProvider({ DB_CONFIG, logger }) { | ||
return MongoClient.connect(DB_CONFIG.uri) | ||
.then(function(db) { | ||
let fatalErrorPromise = new Promise((resolve, reject) { | ||
db.once('error', reject); | ||
}); | ||
logger.log('info', 'db service initialized!'); | ||
logger.log('info', 'db service initialized!'); | ||
return { | ||
servicePromise: db, | ||
shutdownProvider: db.close.bind(db, true), | ||
errorPromise: fatalErrorPromise, | ||
}; | ||
}); | ||
}) | ||
return { | ||
servicePromise: db, | ||
shutdownProvider: db.close.bind(db, true), | ||
errorPromise: fatalErrorPromise, | ||
}; | ||
}); | ||
} | ||
// What if we need 2 mongodb clients? | ||
// Just use service mapping! | ||
constant('DB_CONFIG2', { uri: 'mongo:xxxxx' }); | ||
provider('db2', | ||
// You can wire a dependency with an different name | ||
// than the one expected by your service provider with | ||
// the mapping feature | ||
depends(['DB_CONFIG2:DB_CONFIG', 'logger'], | ||
dbProvider | ||
); | ||
``` | ||
@@ -280,2 +300,6 @@ | ||
</dd> | ||
<dt><a href="#toMermaidGraph">toMermaidGraph(options)</a> ⇒ <code>String</code></dt> | ||
<dd><p>Outputs a Mermaid compatible dependency graph of the declared services. | ||
See <a href="https://github.com/knsv/mermaid">Mermaid docs</a></p> | ||
</dd> | ||
<dt><a href="#run">run(dependenciesDeclarations)</a> ⇒ <code>Promise</code></dt> | ||
@@ -448,2 +472,34 @@ <dd><p>Creates a new execution silo</p> | ||
``` | ||
<a name="toMermaidGraph"></a> | ||
## toMermaidGraph(options) ⇒ <code>String</code> | ||
Outputs a Mermaid compatible dependency graph of the declared services. | ||
See [Mermaid docs](https://github.com/knsv/mermaid) | ||
**Kind**: global function | ||
**Returns**: <code>String</code> - Returns a string containing the Mermaid dependency graph | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| options | <code>Object</code> | Options for generating the graph (destructured) | | ||
| options.shapes | <code>Array.<Object></code> | Various shapes to apply | | ||
| options.styles | <code>Array.<Object></code> | Various styles to apply | | ||
| options.classes | <code>Object</code> | A hash of various classes contents | | ||
**Example** | ||
```js | ||
import Knifecycle from 'knifecycle' | ||
const $ = new Knifecycle(); | ||
$.constant('ENV', process.env); | ||
$.constant('OS', require('os')); | ||
$.service('app', $.depends(['ENV', 'OS'], () => Promise.resolve())); | ||
$.toMermaidGraph(); | ||
// returns | ||
graph TD | ||
app-->ENV | ||
app-->OS | ||
``` | ||
<a name="run"></a> | ||
@@ -455,3 +511,3 @@ | ||
**Kind**: global function | ||
**Returns**: <code>Promise</code> - Service descriptor promise Returns the decorator function | ||
**Returns**: <code>Promise</code> - Service descriptor promise | ||
@@ -458,0 +514,0 @@ | Param | Type | Description | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
114555
571