folkjs
v1.0.1
Published
A simple dependency injection container
Downloads
4
Maintainers
Readme
Folk.JS
Folk.JS is a simple Dependency Injection (DI) Container for Javascript/Node.
Table of Contents
Installation
Install with npm
npm install folkjs --save
Or yarn
yarn add folkjs
Usage
Folk.JS was created to be simple. At minimun, At minimum, you need to do 3 things:
- Create a container
- Register some modules in it
- Resolve and use!
const Container = require('folkjs');
// Create a DI container
const container = Container();
// Create a factory function
// Argument is the current Container resolver
const makeUserService = ({ db }) => {
return {
create: (user) => db.insert(user);
}
};
// Constants are registered as a Container.CONSTANT by default
const container = require('connString', 'localhost');
// Functions are registered as a Container.FACTORY by default
const DBFactory = ({ connString }) => {
return new Database(connString);
};
// To register Function as a Constant...
container.register('fetch', fetch, Container.CONSTANT);
container.register('UserService', makeUserService);
container.register('db', DBFactory);
// To resolve use `resolve`
const UserService = container.resolve('UserService');
// OR
const UserService2 = container.draw.UserService;
// To register several modules:
container.register({
number1: 10,
number2: 42,
});
console.log(container.draw.number2) // Outputs: 42
API
Author
Felipe J. L. Rita @felipelopesrita