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
The module registering seems nice, is it based on angular's dep injection?
Still currently there's not that much difference from the current api in capability and semantics, only in structure.
For a second iteration of the pub-sub bus I'd like to see a way to do data validation at this layer, either at runtime or leveraging typescript type checking in the future.