node-rules is a module of it's own kind, it's a light weight forward chaining Rule Engine!
Install it:npm install --save node-rules
Usage:
It's a three step process, to get the engine running:
Defining a Rule.
Defining a Fact.
Initialize the rules engine and execute the rule.
Create a rule:
1234567891011
/* Sample Rule to block a transaction if its below 500 */varrule={"condition":function(R){R.when(this.transactionTotal<500);},"consequence":function(R){this.result=false;this.reason="The transaction was blocked as it was less than 500";R.stop();}};
Create a fact:
1234567
/* Fact with less than 500 as transaction, and this should be blocked */varfact={"name":"user4","application":"MOB2","transactionTotal":400,"cardType":"Credit Card"};
Initialize the rules engine:
123
/* Creating Rule Engine instance and registering rule */varR=newRuleEngine();R.register(rule);