https://github.com/junosuarez/nali
npm module: a DI service registry
https://github.com/junosuarez/nali
Last synced: 9 months ago
JSON representation
npm module: a DI service registry
- Host: GitHub
- URL: https://github.com/junosuarez/nali
- Owner: junosuarez
- License: mit
- Created: 2013-08-02T02:56:36.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2015-11-12T04:43:11.000Z (over 10 years ago)
- Last Synced: 2025-02-15T16:46:19.440Z (over 1 year ago)
- Language: JavaScript
- Size: 234 KB
- Stars: 1
- Watchers: 8
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# nali
a service registry for dependency injection and application composition
[](https://github.com/feross/standard)
[](https://david-dm.org/jden/nali)
[](https://circleci.com/gh/jden/nali)
## usage
```js
const nali = require('nali')
// we can register services
// type Service: (...) => Instance
// Services are functions with
// optional dependency names as parameters
// and which return an initialized instance of itself
// the return value can be a Promise or a Value
nali.register({
config: function () {
return P
conn_str: 'http://foo.bar'
},
db: function (config) {
return Doodaboos.connect(config.conn_str)
})
// we can resolve an instance of our `db` service:
nali.resolve('db').then(function (db) {
// now we can do stuff with our db service
})
```
nali will fetch or lazily instantiate arbitrary trees of service dependencies.
## api
### `nali(name: String) => service:any`
(alias: `nali.fetch`)
Synchronously fetch a service instance
Can also be used to asynchronously resolve multiple dependencies, for example:
```js
nali(function (db, log, sessions, webService) {
// now we have all the things
})
```
### `nali.resolve(name: String) => Promise`
Asynchronously resolve an instance of a service, lazily instantiating any dependencies if necessary
### `nali.registerInstance(name: String, instance: Object) => void`
*deprecated* use `nali.register('name', () => instance)` instead
### `nali.registerService(name: String, service: Service) => void`
*deprecated* use `nali.register('name', service)` instead
## Service Dependencies
DI works similar to Angular. Service init functions state the names of their dependencies as parameter names, which are parsed out when the Service is registered. They are not called with `new` or with any particular `this` context.
Return a Promises/A+ promise if you need to asynchronously instantiate a service
## Resolution algorithm
Work in progress.
Try get local instance
Try get new instance of local service
Try get parent instance
Try get new instance of parent service
Etc
## not implemented
- scopes
- disposing instances / lifecycle management
- anything else
## installation
$ npm install nali
## running the tests
From package root:
$ npm install
$ npm test
## contributors
- jden
## license
MIT. (c) MMXV AgileMD. See LICENSE.md