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
fakeswhen writing tests. - Support for service providers, to bind dependencies in structured way.
Installation
You can install the package from npm.
npm i --save foldloaderSetup
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 isuseandmake
function use
use(<namespace>)- file in node_modules
- or file in namespace defind in
autoloadswith 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
bindbind file with file or objectclass Foo { } ioc.bind('App/Foo', function () { return new Foo() })class Foo { } ioc.bind('App/Foo', function () { return Foo() })method
singletonfile with file or object asbindbut isdesign pattern singletonmethod
aliascreate sholt nameioc.alias('Model/Foo', 'Foo')method
useandmakemethod
fakeandsingletonFakereqlace namespace have exitsIoc.fake('Adonis/Src/Lucid', function () { return FakeModel })method
restorenamespace registerIoc.restore('Adonis/Src/Lucid') Ioc.restore('Adonis/Src/Lucid', 'Adonis/Src/Config') Ioc.restore() // restore all
Using resolver
- method
forDirget directories was config inpackage.json
const closure = ioc.use(resolver.forDir('app').translate(closure))- method
translateTranslate binding using resolver translate
const closure = ioc.use(resolver.forDir('app').translate(closure))- method
resolveResolves the binding from the IoC container. This method is a combination oftranslateandIoc.makefunction.
// class App/User
const handler = resolver.resolveFunc('App/User')- method
resolveFuncResolves a function by translating the binding and then validating the existence of the method on the binding object. Also if thebindingparam 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
registerwill 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.jsanywhere
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 instanceSimple 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
Fooclass 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:winExtends
adonis-fold – @adonis-fold – virk@adonisjs.com