/
main.ts
51 lines (42 loc) 路 1.02 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import {
RuleEngineService,
Rule,
Condition,
Action,
RuleBuilder,
} from '@nestify/rule-engine';
@Rule({ name: 'test', description: 'A test rule' })
class TestRule {
@Condition()
async condition(facts: any) {
return 'test' === facts['value'];
}
@Action({ order: 1 })
async action1(facts: any) {
facts.value = 'action1';
console.log('action1', facts);
}
@Action({ order: 2 })
async action2(facts: any) {
console.log('action2', facts);
}
}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const ruleEngine = app.get(RuleEngineService);
const rules = [];
rules.push(ruleEngine.register(TestRule));
rules.push(
new RuleBuilder()
.name('test2')
.priority(1)
.when(async () => true)
.then(async () => console.log('test2 action'))
.build(),
);
await ruleEngine.fire(rules, { value: 'test' });
await app.listen(3000);
}
bootstrap();