foldloader v0.0.3
FoldLoader 🚀
Dependency manager, autoload and IoC container for Node.js
Features
- Support for binding dependencies with unique namespaces.
- Autoloading multiple directories under a namespace.
- Defining aliases for bindings.
- Automatic resolution of namespaces and transparent dependency injection.
- Support for
fakes
when writing tests. - Support for service providers, to bind dependencies in structured way.
Installation
You can install the package from npm.
npm i --save foldloader
Setup
using require
Require in main run
require('flodloader')
Config in file package.json
{
"autoload": {
"root": "App",
"directories": {
"app": "./src",
"test": "./test"
},
"preLoadFiles": [
"./start.js"
],
"autoloads": {
"App": "./src",
"Test": "./test"
}
}
}
root
: namespace Root of appdirectories
: defind folder using
const closure = ioc.use(resolver.forDir('app').translate(closure))
preLoadFiles
: files load after run config autoloadautoloads
: defind namespace of folder
Basic Usage
- When
require('flodloader')
in main file, it will register two function globle isuse
andmake
function use
use(<namespace>)
- file in node_modules
- or file in namespace defind in
autoloads
with structurenamespace + file || namespace + folder + file
function make
- require file, constructor and inject to class
class Foo {
static get inject () {
return ['App/Bar']
}
constructor (bar) {
this.bar = bar
}
}
const fooInstance = Ioc.make(Foo)
Namespace default Autoload
const { ioc, resolver, registrar } = use('Autoload')
Using ioc
method
bind
bind file with file or objectclass Foo { } ioc.bind('App/Foo', function () { return new Foo() })
class Foo { } ioc.bind('App/Foo', function () { return Foo() })
method
singleton
file with file or object asbind
but isdesign pattern singleton
method
alias
create sholt nameioc.alias('Model/Foo', 'Foo')
method
use
andmake
method
fake
andsingletonFake
reqlace namespace have exitsIoc.fake('Adonis/Src/Lucid', function () { return FakeModel })
method
restore
namespace registerIoc.restore('Adonis/Src/Lucid') Ioc.restore('Adonis/Src/Lucid', 'Adonis/Src/Config') Ioc.restore() // restore all
Using resolver
- method
forDir
get directories was config inpackage.json
const closure = ioc.use(resolver.forDir('app').translate(closure))
- method
translate
Translate binding using resolver translate
const closure = ioc.use(resolver.forDir('app').translate(closure))
- method
resolve
Resolves the binding from the IoC container. This method is a combination oftranslate
andIoc.make
function.
// class App/User
const handler = resolver.resolveFunc('App/User')
- method
resolveFunc
Resolves a function by translating the binding and then validating the existence of the method on the binding object. Also if thebinding
param is a function, it will be recognized and returned.
// with `find` as method of App/User
const handlerInstance = resolver.resolveFunc('App/User.find')
Using registrar
- register provider class extendes to
- all method
register
will call after all methodboot
const { ServiceProvider } = require('flodloader')
class WsProvider extends ServiceProvider {
register () {
// this app as ioc
this.app.singleton('Adonis/Addons/Ws', (app) => {
const Ws = require('../src/Ws')
const Config = app.use('Adonis/Src/Config')
const Context = app.use('Adonis/Addons/WsContext')
const Server = app.use('Adonis/Src/Server')
return new Ws(Config, Context, Server)
})
}
boot () {
}
}
- Have register Provider
// add provider
const { registrar } = use('Autoload')
registrar.providers(arrayProvider)
reiretrar.register()
Base
with structure folder
App
| index.js
| Model
| User.js
| Helpers
| index.js
start.js
index.js
package.json
- we have require
Model/User.js
anywhere
const foo = ioc.use('Model/User')
Using bind
const { ioc } = use('Autoload')
class Foo {
}
ioc.bind('App/Foo', function () {
return new Foo()
})
const foo = ioc.use('App/Foo')
// return Foo class instance
Simple enough! But we do not see the real power of the Ioc container, since we can instantiate the class manually too. Right? NO
Here are the following benefits.
The author of the
Foo
class can decide how to instantiate the class and return a properly configured instance, instead of leaving it to the consumer.While you are making use of the Ioc container, one binding can be dependent upon others, without much work. For example
class Foo {
constructor (config) {
//
}
}
ioc.bind('App/Foo', function (app) {
const config = app.use('App/Config')
return new Foo(config)
})
const foo = ioc.use('App/Foo')
Tests
Tests are written using japa. Run the following commands to run tests.
npm run test:local
# report coverage
npm run test
# on windows
npm run test:win
Extends
adonis-fold – @adonis-fold – virk@adonisjs.com