Necessarily Precedes Mischief
Miss any of our Open RFC calls?Watch the recordings here! »

folkjs

1.0.1 • Public • Published

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

Install

npm i folkjs

DownloadsWeekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

4.7 kB

Total Files

4

Last publish

Collaborators

  • avatar