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

commonjs-injector

v0.4.1

Published

A dependency injection system for node.js

Downloads

23

Readme

commonjs-injector

Dependency injection module system

Example using a sails.js model and def-inc module

  • Require the injector globally so there is a single location to modify the configuration of the injector.
# App start point
global.injector = require('commonjs-injector').setEnv('production')
  • Use it as a wrapper of your module, it will work pretty much the same as a current commonjs module, with the exception of the injector call, and that calls to @import that will work similar to node require()
# api/abstract/AuthModel.coffee
injector.set ->
  # Module Dependencies
  def = @import('def-inc')

  # Module
  @authModel = def.Obj(
    encrypPassword: ->
      #some code
  )

module.exports = injector.get()

You can omit injector.get() by exporting what is returned by injector.set

# api/abstract/AuthModel.coffee
module.exports = injector.set ->
  # Module Dependencies
  def = @import('def-inc')

  # Module
  authModel = def.Obj(
    encrypPassword: ->
      #some code
  )
  
  return authModel
  • Here we import more modules, @import, works almost the same as require which this fn uses internally.
# api/models/Product.coffee
injector.set ->
  # Module Dependencies
  def       = @import('def-inc')
  baseModel = @import(__dirname, './abtract/baseModel') # Relative to the folder
  AuthModel = @import('./abstract/AuthModel') # Relative to cwd
  roles     = @importGlobal('roles') # gets global.Roles

  # Module
  @products = def.Obj(
    include_: [baseModel, AuthModel]
    attributes:
      name: {type: 'string', required: yes,  unique: true}
      description: {type: 'string'}
      slug: {type: 'string'}

    schema: true

    beforeCreate: (values, next)->
      values.slug = Tools.slugify(values.name)
      next()
  )

module.exports = injector.get()

Inject dependencies

  • Now in our tests, we configure the injector to use externally injected modules by setting its environment as testing, this means, that every call to require, should return a function that accepts an object, which keys are the name of the module (filename) and its values are the module itself (or a mock obj, or anything you want), you can inject 0 or more dependencies, those that are not specified will use the ones defined in the module itself.
global.injector.setEnv('testing')

defMock = {Obj: -> console.log 'mocked def'}
authModelMock = {}
rolesMock = {}

baseModel = require('./abtract/baseModel')({
 'AuthModel': authModelMock,
 'def-inc': defMock
 'roles': rolesMock
})

Caveat: The whole idea of this module is to use all your modules as you normally do, and when you want to test, you disable the bypassing by changing the environment or directly calling "byPassInjection(boolean)", but bare in mind that node require caches files already required, so setting injector.byPassInjection(false) after requiring a module that uses the injector will use the default value if called later and not the new setted value, this is not a bug, is the expected behavior since we don't want to disable caching, in that case you can remove the item from the cache.

###Changelog###

  • 0.4.0: Now you don't have to set @dependencies on the arguments list, this benefits js users since now there is nothing to set