Code Monkey home page Code Monkey logo

konteiner's Introduction

konteiner - simple zero-dependencies DI container for node.js apps

Warning

This package is no longer maintained and will not receive further updates.

Build Status Coverage Status

This module provides you means to:

  • register desired instantiable modules (functions, classes) to DI container
  • use the initialized dependencies in modules by obtaining them via getter on Konteiner instance, that is provided via constructor/parent function
  • having the modules initialized in lazy manner, ie. on first konteiner.get call

Usage

  • Install the dependency npm i --save @petrmiko/konteiner@latest
  • In JS code
const Konteiner = require('@petrmiko/konteiner')

const konteiner = new Konteiner()

// first we need to have some instance creators, here functions
const Logger = () => console
const Messenger = /** @type {Konteiner} */ (konteiner) => {
	const logger = konteiner.get(Logger)
	return {
		sendMessage(text) { logger.log(text) }
	}
}
// following lines will just register dependencies, init is made upon first get for affected dependencies
konteiner.register(Logger)
konteiner.register(Messenger)

const messenger = konteiner.get(Messenger) // this will actually invoke the constructor of Messenger (and Logger, since it is a dependency of demoMessenger)
messenger.sendMessage('Hello world!') // console.log will print out 'Hello world!'

If you want to load all dependencies in an directory, you can also do following.

const Konteiner = require('@petrmiko/konteiner')
const konteiner = new Konteiner({exclude: [
	'\\.test\\.' // all test files will be omitted from batch loading using .registerPath
]})

konteiner.registerPath('./src', {exclude: [
	'\\.test\\.',
	'index\\.js'
]}) // all but tests and index.js will be loaded. Overrides exclude from constructor for this call only

// then in place of use we need to know, what dependency creator was used to retrieve its instance
const Service = require('./src/service')

const someService = konteiner.get(Service) // all dependencies bound to Service will be now initialized
...

For more details, see API section.

konteiner's People

Contributors

dependabot[bot] avatar petrmiko avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.