npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zander

v0.2.0

Published

Node.JS dependency injection framework

Downloads

1

Readme

Zander

version npm dependencies devDependency Status Build Status

A configurable dependency injection for Node.JS without a need to patch require or manually injecting references. Simple, easy to get started and bundled with examples to bootstrap your project.

Usage

Install zander using your favorite package manager : npm

npm install zander --save

Once you have configured zander in your project, here is the next step.

Javascript

"use strict";
var zander = require('zander');
var path = require('path');
var configLoader = new zander.SimpleFilePathMatchLoader(["modules/module.json"]);
var depManager = zander.DependencyInjection({ configLoader: configLoader, modulePath: path.join(__dirname, 'modules') });
depManager.configure();

Note that the configure function returns Promise which can be used to get references of beans or perform any activity which requires all the beans are in ready state. The examples could include start listing on 8080 port after all services are ready. Refer to following code block for details

depManager.configure().then(function (configured) {
    console.log("All beans are initialized and injected.");
    var customerBean = depManager.getBean("customer");
    customerBean.greet();
});

Multiple beans under one folder

In case, if you have multiple beans under one folder due to the grouping requirements your project has, Zander offers path attribute for bean declaration. Refer to multi-bean example for more details.

Module configuration

Zander supports both simple path matching and glob style path matching support for loading configuration files. Just replace SimpleFilePathMatchLoader with WildcardFilePathConfigLoader and you can specify wildcard path.

Split configuration

Zander supports splitting configuration of beans across multiple files and its programmed to identify dependencies in correct order. As a developer you are free to split configuration as per logical groups of beans. For e.g. all service beans in one file, while all business logic and data access bean definitions in another file.

Zander in action

Zander doesn't require to inherit nor make any references to the core framework in your beans. The current implementation supports injecting dependencies using constructors.

A typescript project with zander integration can be found at - Zander-simple

Example

Lets get started with zander.

First create a project with following command

npm init

This will create NodeJS project in the current directory by creating package.json. Issue following command to install zander.

npm install zander --save

Once the installation is complete, create a new folder modules. This directory will serve as a folder for grouping testable code that we will create in next few steps. Inside modules folder create module.json file, which will be used to declaratively define and inject our modules. The directory structure will look like below

your_project_dir
├───package.json
├───modules
│   └───module.json
├───node_modules
│   ├───balanced-match
│   ├───bluebird
│   ├───... other dependency folders
│   └───zander

Now create two folders - user and greeting with index.js file in each folder. Also create index.js folder at the root of the project directory. The directory structure will now look like below

your_project_dir
├───index.js
├───package.json
├───modules
│   ├───user
│   │   └───index.js
│   ├───greeting
│   │   └───index.js
│   └───module.json
├───node_modules
│   ├───balanced-match
│   ├───bluebird
│   ├───... other dependency folders
│   └───zander

Open user\index.js file in your favourite editor and paste following code.

"use strict";

var User = (function(){
  function User(greeting){
    this.greeting = greeting;
  }

  User.prototype.greetUser = function(username){
    this.greeting.greet("Hello " + username);
  };

  return User;
})();

module.exports = User;

Open greeting\index.js file and paste following code.

"use strict";

var Greeting = (function(){
  function Greeting(){
  }

  Greeting.prototype.greet = function(message){
    console.log(message);
  };

  return Greeting;
})();

module.exports = Greeting;

Did you notice? User bean is dependent on Greeting bean but it doesn't explicitly uses require function to reference it. Rather it assumes the bean reference will be passed via constructor. Lets complete the dependency part, by entering following code in modules\module.json

{
  "user":{
    "construct":["greeting"]
  },
  "greeting":{
    "construct":[]
  }
}

user and greeting serve as a bean references, if you wish to assign different identifiers (other than directory name), you can change it. But you should also define additional path to indicate where the bean is located. Refer the below configuration with different names. (skip this step, if you do not wish to change the bean names.)

{
  "userBean":{
    "construct":["greetingBean"],
    "path":"user"
  },
  "greetingBean":{
    "construct":[],
    "path":"greeting"
  }
}

Additionally, if you wish to assign different names or want to add multiple beans under one folder, you can do so but enter the name of the file along with folder name without extension. (skip this step, if you do not wish to assign different name to index.js)

{
    "user":{
      "construct":["greetingBean"],
      "path":"user/my_custom_file"
    }
}

Now, open index.js at the root of the project and paste following code.

"use strict";
var zander = require('zander');
var path = require('path');
var configLoader = new zander.SimpleFilePathMatchLoader(["modules/module.json"]);
var depManager = zander.DependencyInjection({ configLoader: configLoader, modulePath: path.join(__dirname, 'modules') });
depManager.configure()
.then(()=>{
  console.log("All beans are created.");
  var userBean = depManager.getBean("user");
  userBean.greetUser("John");
});

Note above code will boostrap zander by specifying the location of the directory of dependency injection and once the injection is complete, you can reference a bean and invoke method on it. Time to run our sample project and check the ouput. Execute following command at the root of the project in command prompt.

node index.js

and you should see following output

All beans are created.
Hello John

Sounds Simple! No messy references in the logical code, no additional coding required to manage dependencies, no need to patch require.