foldloader

0.0.3 • Public • Published

FoldLoader 🚀

Dependency manager, autoload and IoC container for Node.js

NPM Version Build Status Coveralls

Features

  1. Support for binding dependencies with unique namespaces.
  2. Autoloading multiple directories under a namespace.
  3. Defining aliases for bindings.
  4. Automatic resolution of namespaces and transparent dependency injection.
  5. Support for fakes when writing tests.
  6. 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 app
  • directories : defind folder using
const closure = ioc.use(resolver.forDir('app').translate(closure))
  • preLoadFiles : files load after run config autoload
  • autoloads : defind namespace of folder

Basic Usage

  • When require('flodloader') in main file, it will register two function globle is use and make

function use

 use(<namespace>)
  • file in node_modules
  • or file in namespace defind in autoloads with structure namespace + 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 object
class 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 as bind but is design pattern singleton
  • method alias create sholt name
ioc.alias('Model/Foo', 'Foo')
  • method use and make

  • method fake and singletonFake reqlace namespace have exits

  Ioc.fake('Adonis/Src/Lucid', function () {
    return FakeModel
  })
  • method restore namespace register
Ioc.restore('Adonis/Src/Lucid')
Ioc.restore('Adonis/Src/Lucid', 'Adonis/Src/Config')
Ioc.restore() // restore all
Using resolver
  • method forDir get directories was config in package.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 resolveResolves the binding from the IoC container. This method is a combination of translate and Ioc.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 the binding 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 method boot
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.

  1. 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.

  2. 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-foldvirk@adonisjs.com

Package Sidebar

Install

npm i foldloader

Weekly Downloads

7

Version

0.0.3

License

MIT

Last publish

Collaborators

  • nortonperson