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

@ldlework/inversify-config-injection

v0.0.7

Published

Inject your application configuration.

Downloads

5

Readme

inversify-config-injection

Allows the binding of configuration constants provided by the node-config library through the inversify IoC container.

See the node-config documentation for how the configuration files need to be named and various other options like loading them from a yaml file instead of a json.

Eager binding

For the simplest operation you can just load the container module provided by this package. This loads all the compatible configurations from the config file and binds them as constants. By default the keys used for the binding will be the json path to the configuration.

Assuming this is your config/default.json

{
	"app": {
		"db": {
			"host": "localhost",
			"port": 1234,
			"seeds": ["8.8.8.8","8.8.4.4"]
		}
	},
  "other": {
    "foo":"bar"
  }
}

you can annotate your classes like this

@injectable(
class DefaultDatabase implements Database{	
  public constructor(
		@inject("app.db.host") public host: string,
		@inject("app.db.port") public port: number
	){};
}

When you initialise your container also import the default binder module and load it into the container:

import {defaultEagerBinderModule} from 'inversify-config-injection';
const container = new Container();
container.load(defaultBinderModule);

container.bind<Database>("DB").to(DefaultDatabase);
const db = container.get<DefaultDatabase>("DB");

expect(db.host).to.equal("localhost");

Various configuration options for the binder are described below. For this you should instantiate an EagerBinder instead of using defaultEagerBinderModule

import {EagerBinder} from 'inversify-config-injection';
const container = new Container();
const configBinder = new EagerBinder({
  log: true,
  root: 'app',
  prefix: 'cfg',
  objects: true
});
container.load(configBinder.module());

Binding only part of the configuration file

Use the root configuration parameter of the eager binder. This will only load children of this particular path. For our above example:

new EagerBinder({
  root: 'app'
});

only loads the app breanch of the configuration. The keys necessary for injecting are also shortened.

@inject("db.host") public host: string,
@inject("db.port") public port: number

Adding a prefix to the binding key

In order to avoid collisions you can add a prefix to the binding keys. For our above example:

new EagerBinder({
  root: 'app',
  prefix: 'cfg'
});

This makes correct biding:

@inject("cfg.db.host") public host: string,
@inject("cfg.db.port") public port: number

Note that there is no cfg key in the configuration json.

Binding entire objects

In addition to binding every leaf entry of the configuration, you can also bind the intermediary object by turning on objects in the EagerBinder settings.

new EagerBinder({
  root: 'app',
  objects: true
});

This will bind to constants db.host, db.port, db.seeds but also db as the constant object

{
  host: "localhost",
	port: 1234,
	seeds: ["8.8.8.8","8.8.4.4"]
}

Binding logs

For debugging purposes, you can turn on binding logs

new EagerBinder({
  log: true
});

This allows you to get an array of logs with binder.getBindingLog()

console.log( binder.getBindingLog().join("\n") );
Binding 'cfg.db.host' to string 'localhost'
Binding 'cfg.db.port' to number '1234'
Binding 'cfg.db.seeds' to string[] '8.8.8.8,8.8.4.4'