Skip to content

Repository files navigation

Plain Rules Engine

LicenseTypeScript

Introduction

Plain Rules Engine is a TypeScript library for creating and evaluating simple business rules in Node.js applications. It provides a flexible way to define and apply rules to your data.

Features

  • Easy rule creation and evaluation. Defining a rule in JSON is as simple as:
{
"trackHasStrongLanguage": {
"conditions": [
["$.user.age", "lessThan", 16],
["$.tags", "contains", "strong language"]
],
"effect": {
"action": "omit"
}
},
... other rules
}

You can also define rules in TypeScript:

exportconstmusicSearchRules: Rule={trackHasStrongLanguage: {conditions: [['$.user.age',Operator.LESS_THAN,16],['$.tags',Operator.CONTAINS,'strong language'],],effect: {action: Action.OMIT,},},// Add more rules here};

In this case, the effect defined for "trackHasStrongLanguage" will only be applied to objects that meet all conditions in conditions array

  • Customizable rule actions and conditions.
  • Written in TypeScript for type safety.

Installation

You can install Plain Rules Engine via npm:

npm install plain-rules-engine

Usage

Here's an example of how to use Plain Rules Engine:

import{RuleEngine,typeRule,typeApplyRulesResponse,}from'plain-rules-engine';// Define your rules (could be in a JSON file, TS file, an object store, in-memory database etc.). See example rule definition above// Fetch your rules (you'll have to create the function that does that)constmusicSearchRules: Rule=fetchRulesFromDataSource();// Instantiate the rule engineconstengine=newRuleEngine(musicSearchRules);// Define your dataconsttracks=[{title: 'Heal the World',artist: 'Michael Jackson',year: 1991,album: 'Dangerous',tags: ['pop','society'],},{title: 'The Girl From Last Night',artist: 'Jackson Pepper',year: 2017,album: 'Slot Machine',tags: ['rap','strong language'],},];constuser={user: {age: 14,lastLogin: '2023-08-26T16:00:45Z',},};// Apply the rulesconstevaluation=engine.applyRules(tracks,user);// Handle the resultsconsole.log('RESULT: ',evaluation);

For more detailed documentation and examples, please refer to Building a Rule Engine With TypeScript — the article that inspired Plain Rules Engine.

Contribution

We welcome contributions from the community. If you'd like to contribute to this project or have any questions, issues, or suggestions, please feel free to open an issue.

License

This project is licensed under the Apache-2.0 License.

About

A TypeScript library for creating and evaluating simple business rules in Node.js applications. It provides a flexible way to define and apply rules to your data

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages