# typescript-business-rules-engine

> A Typescript rule engine where rules are defined in JSON format.

Latest version **1.1.12** (published 2023-06-28) · GPLV3 license · 0 weekly downloads

## Install

```sh
npm install typescript-business-rules-engine
pnpm add typescript-business-rules-engine
yarn add typescript-business-rules-engine
bun add typescript-business-rules-engine
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.12 |
| Published | 2023-06-28 |
| First published | 2023-05-28 |
| Weekly downloads | 0 |
| License | GPLV3 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 2 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | carlososuna11 |
| Keywords | business, rules, engine, typescript, json |

## Links

- npm: https://www.npmjs.com/package/typescript-business-rules-engine
- Repository: https://github.com/Carlososuna11/business-rules
- Homepage: https://github.com/Carlososuna11/business-rules#readme
- Issues: https://github.com/Carlososuna11/business-rules/issues
- npm.io page: https://npm.io/package/typescript-business-rules-engine

## Dependencies (8)

- [bson](https://npm.io/package/bson.md) ^5.3.0
- [uuid](https://npm.io/package/uuid.md) ^9.0.0
- [moment](https://npm.io/package/moment.md) ^2.29.4
- [@types/uuid](https://npm.io/package/@types/uuid.md) ^9.0.1
- [json-schema](https://npm.io/package/json-schema.md) ^0.4.0
- [plantuml-encoder](https://npm.io/package/plantuml-encoder.md) ^1.4.0
- [@tsconfig/recommended](https://npm.io/package/@tsconfig/recommended.md) ^1.0.2
- [@types/plantuml-encoder](https://npm.io/package/@types/plantuml-encoder.md) ^1.4.0

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.1.12 (latest) — 2023-06-28
- 1.1.10 — 2023-06-21
- 1.1.9 — 2023-06-21
- 1.1.8 — 2023-06-21
- 1.1.7 — 2023-06-21
- 1.1.6 — 2023-06-21
- 1.1.5 — 2023-06-21
- 1.1.4 — 2023-06-21
- 1.1.3 — 2023-06-21
- 1.1.2 — 2023-06-21
- 1.1.1 — 2023-06-19
- 1.1.0 — 2023-06-07
- 1.0.9 — 2023-05-31
- 1.0.8 — 2023-05-31
- 1.0.7 — 2023-05-30
- … 3 more at https://npm.io/package/typescript-business-rules-engine/versions

## README

# TypeScript Business Rules Engine

A Typescript rule engine where rules are defined in JSON format.

_Facts_ are plain JavaScript or JSON objects or objects from ES6 classes with getters and setters.
_Rules_ are specified in pure JavaScript rather than in a separate, special-purpose language like DSL.

## Install

```bash
npm install typescript-business-rules-engine
```

## Usage

This is a basic example.

```ts
// import
import { Engine } from 'typescript-business-rules-engine';

const facts = {
	user: {
		name: 'frank',
		stars: 347,
	},
	weather: {
		temperature: 20,
		windy: true,
		rainy: false,
	},
};

const rules = [
	{
		// if data.user.stars >= 200 then data.user.mood = 'great'
		name: 'Mood Great',
		description: 'mood is great if 200 stars or more',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.greaterEqualThan': [
				{
					'$ctx.get': ['data.user.stars'],
				},
				200,
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.user.mood', 'great'],
			},
		],
	},
	{
		// if data.user.mood === 'great && data.weather.temperature >= 20 && !data.weather.rainy then data.goWalking = true
		name: 'go for wak',
		description: 'go for a walk if mood is great and the weather is fine',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.and': [
				{
					'$op.equal': [
						{
							'$ctx.get': ['data.user.mood'],
						},
						'great',
					],
				},
				{
					'$op.greaterEqualThan': [
						{
							'$ctx.get': ['data.weather.temperature'],
						},
						20,
					],
				},
				{
					'$op.not': [
						{
							'$ctx.get': ['data.weather.rainy'],
						},
					],
				},
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.goWalking', true],
			},
		],
	},
	{
		// if wheater.temperature < 20 then data.user.mood = 'bad'
		name: 'Mood Bad',
		description: 'mood is bad if temperature is below 20',
		priority: 0,
		final: false,
		activationGroup: '',
		condition: {
			'$op.lessThan': [
				{
					'$ctx.get': ['data.weather.temperature'],
				},
				20,
			],
		},
		preActions: [],
		postActions: [
			{
				'$ctx.set': ['data.user.mood', 'bad'],
			},
		],
	},
];

const main = async () => {
	const engine = new Engine('Test Engine', rules);

	console.log(await engine.evaluate(facts));
	/*{
    elapsed: 6,
    fired: [
        {
        name: 'Mood Great',
        fired: true,
        discarted: false,
        actions: [Array]
        }
    ],
    context: { data: { user: [Object], weather: [Object] }, extra: {} }
    } 
    */
};

main();
```

These are the resulting facts:

```javascript
{
    elapsed: 6,
    fired: [
        {
        name: 'Mood Great',
        fired: true,
        discarted: false,
        actions: [Array]
        }
    ],
    context: { data: { user: [Object], weather: [Object] }, extra: {} }
    }
```

## Features

### Rule engine

The engine utilizes forward-chaining methodology and operates within a typical match-resolve-act cycle. It endeavors to infer the maximum amount of information feasible from the provided facts and regulations. If there is no additional knowledge to acquire, the process concludes.

### Final rules

For optimization purposes, it can be useful to stop the engine as soon as a specific rule has fired.
This can be achieved by settings the respective rules' property `final` to `true`.
Default, of course, is `false`.

### Activation groups

Only one rule within an activation group will fire during a match-resolve-act cycle, i.e.,
the first one to fire discards all other rules within the same activation group.
Use the rule's `activationGroup` property to set its activation group.

---
_Source: https://npm.io/package/typescript-business-rules-engine · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
