Node-rules

node-rules

Business Rules Engine for JavaScript.

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:

1
2
3
4
5
6
7
8
9
10
11
/* Sample Rule to block a transaction if its below 500 */
var rule = {
    "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:

1
2
3
4
5
6
7
/* Fact with less than 500 as transaction, and this should be blocked */
var fact = {
    "name": "user4",
    "application": "MOB2",
    "transactionTotal": 400,
    "cardType": "Credit Card"
};

Initialize the rules engine:

1
2
3
/* Creating Rule Engine instance and registering rule */
var R = new RuleEngine();
R.register(rule);

Execute the rule:

1
2
3
4
5
6
7
R.execute(fact, function(data) {
    if (data.result) {
        console.log("Valid transaction");
    } else {
        console.log("Blocked Reason:" + data.reason);
    }
});

GIF FTW!

Thanks to Mithun Satheesh for this adventurous module.

Suggest a module

Comments