Rules Engine in node.js
In this article, I will explain the rules engine and how to implement it in our node.js application.
What is the Rule Engine?
A business rules engine is a software system that executes one or more business rules in a runtime production environment. — wiki
Using the rules engine, you no need to change the business logic of your code as and when the changes required. User setup rules from the Frontend. Based on the rules, the application works.
When should we use the Rule Engine?
When we feel our traditional approach is not helpful at the time we can Rule Engine. Below I have given rules
- The logic changes often
- The problem is too complex to solve using the traditional method.
Implement with Node.js application
Project initial setup and install plugins
$ npm install fastify json-rules-engine
json-rules-engine
is a powerful, lightweight rules engine. Rules are composed of simple JSON structures, making them human-readable and easy to persist.
Package.json
Setup index.js code
Write the code for the rule engine
In the above code, we add a condition for temperature that equal to 100. And pass facts as a parameter and check whether it matches or not. If the condition matches successfully it calls inside the onSuccess() block. If the condition matches failed it call inside the onFailure() block.
Reference links
Git code https://github.com/EdisonDevadoss/demo-rules-engine