ModRain (Module Rain) modular architecture framework for modular client and server applications written in javascript/typescript. Use Modrain to implement microservices architecture for client and server side javascript apps.
ModRain
- allows to write fully modularized javascript applications.
- can be used for large scale frontend and node.js backend applications were reusability is expected.
- prevents from npm version hell (multiple modules require each other)
- allows to loosely couple modules and swap implementations at runtime
Create new module (node.js app)
mkdir mymodule
cd mymodule
npm init
npm install modrain --save
touch index.js
var modrain = require("modrain");
var MODULE_NAME = "user";
var ACTION = "create";
// Register new module in namespace
var userModule = modrain.registerModule(MODULE_NAME);
// We can unregister module as well
//var userModule = modrain.unregisterModule(MODULE_NAME);
// Using method interface
userModule.registerHandler(ACTION, function(userObject, callback){
// Implementation to save userObject
// ...
callback(null, "success");
// In case of error
callback("error");
});
TODO Change API to register module with entire module interface passed as second parameter.
var modrain = require("modrain");
modrain.getModule(user).call("create", userObject, function(err, result){
// impl
});
// if global mount option is enabled
modrain.user.create(userObject, function(err, result){
// impl
});
var modrain = require("modrain");
modrain.getModule(user).call("create",userObject).then(function(err, result){
// impl
});
ModRain allows us to register modules into module registry and then define complete set of the actions on each module.
- Callbacks
- Promises
Classical callback interface is supported by default To enable promises use:
npm install --save bluebird
modrain.usePromises(true);
Modules can expose functionality to our application.
ModRain defines abstraction layer for module communications. Modules can interact with each other using transport in the background. Currently library supports following transports:
- Direct method calls (globals)
- Observer (ReactiveComponents)
- Publish/Subscribe (using mediator)
- HTTP (TODO)
Using promise library
Using mediator pattern and topics to register for some actions
ModRain is under active development. API may change.
TODO
@wtrocki Apart from using a network protocol there's no advantage to switching between in-process communication mechanism if you're using the same api.
For instance switching between direct method calls and RxJS in a way that's transparent to client code just changes the performance profile since you can't leverage RxJS' extra features without using a specific api.
There's no need to use a flag and having a runtime switch between different interfaces would break code.
I personally vote for going full-promises, there's no need to support both.
Libraries that do so usually do something like this (very simplified):