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

beat

v1.0.6

Published

Simple dependency injection for node

Downloads

52

Readme

beat Build Status Code Climate

Simple dependency injection for node

NPM

Enjoy also beat-conf, lean configuration utility for Beat

Example

var Beat = require('beat');
var app = new Beat('app');

app.value('port', process.env.PORT || 3000);
app.value('express', require('express'));
  
app.factory('app', function(express){
  return express();
});
  
app.run(function(app, port){
  app.get('/hello.txt', function(req, res){
    res.send('Hello World');
  });
  app.listen(port);
  console.log('Express running at :'+port);
});

How to use

Install it with NPM:

npm install --save beat

Then require it:

var Beat = require('beat');

API

constructor(alias): starts a new Beat with an alias (defaults to "unnamed")

To produce the instance, Beat should be called with new operator.

var myServer = new Beat('server');

value(token, value): defines a value for injection

Register the final value.

myServer.value('port', 80);

factory(token, factoryFn): defines a factory for injection

To produce the instance, factoryFn will be called once (with instance context) and its result will be used.

The factoryFn function can use injection annotation.

myServer.factory('port', function(){
  var port = 80;
  // some logic here
  return port;
});

run(fn): runs a code block, with injection

fn will be called (with instance context).

The fn function can use injection annotation.

myServer.run(function(server, port){
  server.listen(port);
});

get(token): obtains a property

myServer.value('port', 80);
var port = myServer.get('port');

load(beatInstance): import properties and factories from an Beat instance

Useful to bind different beats

var config = new Beat('config');
config.value('port', 80);

myServer.load(config);
myServer.run(function(app, port){
  app.listen(port);
});

or at different files

var config = module.exports = new Beat('config');
config.value('port', 80);
myServer.load(require('./config'));
myServer.run(function(app, port){
  app.listen(port);
});

Annotation

Methods run and factory can recieve annotated functions as arguments, that will be used for injection.

The injector looks up tokens based on argument names:

myServer.run(function(server, port) {
  // will inject objects bound to 'server' and 'port' tokens
});

You can also use a array:

myServer.run(['http', 'serverPort', function(server, port) {
  // will inject objects bound to 'http' and 'serverPort' tokens
}]);

Dependencies

Beat instantiation can declare modules as dependencies.

Therefore, declare them as array at constructor second parameter:

var Beat = require('beat');
var db = module.exports = new Beat('db', ['mongoose']);

db.factory('db', function(mongoose, conf) {
  if(conf.env == 'test')
    mongoose.set('debug', true);
  return mongoose.createConnection(conf.mongo).once('open', function() {
    console.log('Mongoose connected');
  });
});

You can also use objects for aliasing:

var Beat = require('beat');
var db = module.exports = new Beat('db', ['mongoose', {conf:'../config.json'}]);

db.factory('db', function(mongoose, conf) {
  if(conf.env == 'test')
    mongoose.set('debug', true);
  return mongoose.createConnection(conf.mongo).once('open', function() {
    console.log('Mongoose connected');
  });
});

file paths starting with / will be relative to process cwd.

If the required module provides a Beat object, their properties will be mixed with local ones:

var Beat = require('beat');
var routes = module.exports = new Beat('routes', [
  '/lib/middlewares',
  '/lib/models',
  '/lib/app'
]);

routes.factory('routes', function routes(app, authMiddleware, ProductsModel){
  app.all('/api/*', authMiddleware);
  app.get('/api/products/:id', function show(req, res) {
    ProductsModel.findById(req.params.id, function(err, doc) {
      res.send(err?400:(doc||404));
    });
  });
});

Is also possible to load dependencies after construction, with the same sintax:

var Beat = require('beat');
var routes = module.exports = new Beat('routes');

routes.loadModules([
  '/lib/middlewares',
  '/lib/models',
  '/lib/app'
]);

Inspiration

Angular.js DI

Bitdeli Badge

githalytics.com alpha