Socket
Socket
Sign inDemoInstall

adonis-lodge

Package Overview
Dependencies
6
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    adonis-lodge

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


Version published
Weekly downloads
2
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

FAQs

Last updated on 15 Aug 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc