Gathering detailed insights and metrics for magnum-di
Gathering detailed insights and metrics for magnum-di
Gathering detailed insights and metrics for magnum-di
Gathering detailed insights and metrics for magnum-di
A super simple Key,Value Dependency Injection framework for NodeJS
npm install magnum-di
Typescript
Module System
Node Version
NPM Version
TypeScript (99.01%)
JavaScript (0.99%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
7 Stars
89 Commits
2 Watchers
1 Branches
1 Contributors
Updated on Dec 25, 2019
Latest Version
6.0.0
Package Id
magnum-di@6.0.0
Unpacked Size
34.73 kB
Size
8.86 kB
File Count
15
NPM Version
5.6.0
Node Version
8.10.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1npm install --save magnum-di
1//Load it up./ 2var injector = require('magnum-di')() 3 4//Register some dependencies. 5injector.service('MyService', {name: 'Bob', sayName: function(){console.log(this.name)}}) 6 7//inject them into your function 8injector.inject(function(MyService){ 9 MyService.sayName() // -> Bob 10})
Here we register a dependency in our main application file, and use the injector to call the function returned by the require('./UserModel) call.
Run this with npm run example-server
1 2//userRoutes.js 3 4module.exports = function(Router, User) { 5 6 //Here Router and User will be injected. 7 Router.get('/', function(req, res, next){ 8 User.userDetails('Bob', function(err, data){ 9 res.json({user: data}) 10 }) 11 }); 12 13 return {path: '/user', router: Router} 14}; 15 16 17//app.js 18 19var injector = require('magnum-di')(); 20var express = require('express'); 21var Database = require('./Database'); 22var app = express() 23var http = require('http'); 24 25//Register some dependencies. 26injector.factory('Router', express.Router); 27injector.service('Database', Database); 28 29//Here we are letting the injector provide the database to our UserModel 30//This will make it simple to mock in our unit tests. 31var User = injector.inject(require('./UserModel')); 32injector.service('User', User); 33 34var userRoute = injector.inject(require('./UserRoutes')); 35 36app.use(userRoute.path, userRoute.router); 37 38http.createServer(app).listen(8080);
Following our above example, we use magnum-di to mock out our Database object for testing.
Run this with npm run example-test
1 2var injector = require('magnum-di'); 3var should = require('should'); 4 5//Our mock database object 6injector.service('Database', { 7 User: { 8 users: {George: {name: 'George', age: 30}, Mike: {name: 'Mike', age: 20}}, 9 find: function(username, cb) { 10 var user = (this.users[username]) ? this.users[username] : null 11 cb(null, user); 12 } 13 } 14}); 15 16//We now have a user model that is using our mock database. 17var User = injector.inject(require('./UserModel')); 18 19describe('Model being tested with a mock Database object', function() { 20 21 it('should not have real data', function(done) { 22 User.userDetails('Bob', function(err, data){ 23 should(data).be.null 24 done() 25 }) 26 }); 27 28 it('should have mock data', function(done) { 29 User.userDetails('George', function(err, data){ 30 data.age.should.equal(30) 31 done() 32 }) 33 }); 34 35}); 36
Kind: global class
MagnumDI
*
function
function
*
*
| null
Object
boolean
*
Provides an instance of the Magnum DI injector.
MagnumDI
Creates a Child instance of the current injector. Calls to .get or .inject will first search this injector, if a parameter is found it will return it if not it will continue up the tree until a value is found or the topmost instance is reached.
Kind: instance method of MagnumDI
*
Registers an object, string, number or function.
Kind: instance method of MagnumDI
Returns: *
- Returns provided dependency
Param | Type | Description |
---|---|---|
name | string | Name to be used in the injected function |
item | object | array | number | string | function | Item to be injected. |
function
When injected, calls as a constructor with new.
Kind: instance method of MagnumDI
Returns: function
- Returns provided function
Param | Type | Description |
---|---|---|
name | string | Name to be used in the injected function. |
fn | function | Function to be called with new. |
function
When injected, calls the passed function. Returns the result of that call.
Kind: instance method of MagnumDI
Returns: function
- Retuens provided function.
Param | Type | Description |
---|---|---|
name | string | Name to be used in the injected function |
fn | function | Function to be called by injector. |
*
Merges supplied object into the object registered for
Kind: instance method of MagnumDI
Returns: *
- Returns provided dependency
Param | Type | Description |
---|---|---|
name | String | Dependency object to modify |
merge | Object | Object to merge with existing dependency |
*
| null
Returns the specified dependency.
Kind: instance method of MagnumDI
Returns: *
| null
- The named dependency item, or null.
Param | Type | Description |
---|---|---|
name | string | Dependency to retrieve. |
Object
Modifies a registered service object.
Kind: instance method of MagnumDI
Returns: Object
- Replaced dependency
Param | Type | Description |
---|---|---|
name | String | Dependency object to modify. |
replacement | Object | Object to replace current registered object. |
boolean
Removes the specified dependency.
Kind: instance method of MagnumDI
Returns: boolean
- The result of the operation.
Param | Type | Description |
---|---|---|
name | string | Registered dependency to remove. |
*
Runs the given function with args injected and with an optional context object.
Kind: instance method of MagnumDI
Returns: *
- Returns the result of the called function.
Param | Type | Description |
---|---|---|
fnOrstr | function | string | function - inject args and run, string - Return the named dependency. |
thisArg | object | Calling context. |
Run tests with npm test
.
Regenerate this Readme with npm run readme
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
75 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More