adonis-lodge

1.0.1 • Public • Published

Lodge

Lodge is a namespacing library for node/io js. It let you register modules inside your directories under a given namespace.

Why namespacing ?

As Es6 is taking place into our repositories and javascript on the server needs to be more organized , we should namespace our es6 classes to maintain isolated references to each module inside the application.

How it works

Simply autoload es6 classes inside your directories, give them a unique namespace and while consuming do not require them as node modules instead resolve them from Ioc container

Example ( project structure )

├── app/
│ ├── controllers/
│ │ ├──  UserController.js
│── server.js

Namespacing controllers

const Lodge = require("adonis-lodge");
// register controllers under App/Controllers namespace
Lodge.namespace("App/Controllers").register(__dirname + '/app/controllers');
 
// get UserController instace
let UserController = yield Lodge.resolve("App/Controllers/UserController")

In above example we autoloaded all files inside app/controllers method and registered them with a unique namespace to avoid name collisions.

Next we can get the instance of the class as a es6 generator and can call any methods registered inside class.

DI ( Dependency Injection )

DI auto resolves your classes dependencies and inject them into your constructors.

class UsersService{
    *getUsers(){
        return yield some_db_method()
    }
}
 
class UsersController{
    static get inject(){
        return ["UsersService"];
    }
    constructor(UsersService){
        this.usersService = UsersService
    }
    *list(){
        return yield this.usersService.getUsers()
    }
}

DI is combination of constructor method and inject property. First we inject our dependencies using a static method and next we consume them inside our constructor in same order.

Not only it makes things more readable and object oriented , it makes testing easier by mocking constructor arguments.

Using identifiers

Namespacing identifiers gives identifiers to a directory, making resolve process a little easier. Let's take our first example of registering controllers under a namespace but this time with the help of identifiers

const Lodge = require("adonis-lodge");
// register controllers under App/Controllers namespace
Lodge.identifier("controllers").namespace("App/Controllers").register(__dirname + '/app/controllers');
 
// get UserController instace
let UserController = yield Lodge.under("controllers").resolve("UserController")

I hope you can notice the change , now while resolving module/class we do not have to enter the full namespace, as we can easily resolve the class name directly using identifiers.

Api methods

const Lodge = require("adonis-lodge");
Lodge
    .identifier()  // give identifier to directory going to be registered
    .namespace()   // use a unique namespace
    .register()    // directory to register
 
Lodge
    .under()      // identifier namespace to use
    .resolve()    // resolve binding from Ioc container
    
Lodge
    .list()       // dump list of all bindings
    
Lodge
    .clear()      // clear all old bindings

Readme

Keywords

none

Package Sidebar

Install

npm i adonis-lodge

Weekly Downloads

1

Version

1.0.1

License

ISC

Last publish

Collaborators

  • amanvirk