@duraflows/nestjs 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +139 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @duraflows/nestjs
|
|
2
|
+
|
|
3
|
+
[NestJS](https://nestjs.com/) module for [duraflows](https://github.com/camcima/duraflows), providing dependency injection, services, and optional REST controllers for the durable workflow runtime.
|
|
4
|
+
|
|
5
|
+
Part of the [duraflows](https://github.com/camcima/duraflows) monorepo.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Dynamic NestJS module with `forRoot()` and `forRootAsync()` configuration
|
|
10
|
+
- `WorkflowService` for creating instances, triggering events, and querying state
|
|
11
|
+
- `WorkflowTimeoutService` for processing expired workflows
|
|
12
|
+
- Optional REST controllers for full HTTP API
|
|
13
|
+
- `@WorkflowCommand` decorator with automatic discovery
|
|
14
|
+
- Re-exports all types from `@duraflows/core` for convenience
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @duraflows/core @duraflows/nestjs
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
You will also need a persistence adapter such as [`@duraflows/pg`](https://www.npmjs.com/package/@duraflows/pg):
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @duraflows/pg pg
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
### Module Registration
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { Module } from "@nestjs/common";
|
|
34
|
+
import { Pool } from "pg";
|
|
35
|
+
import { WorkflowModule } from "@duraflows/nestjs";
|
|
36
|
+
import { pgWorkflowProviders } from "@duraflows/pg";
|
|
37
|
+
|
|
38
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
39
|
+
|
|
40
|
+
@Module({
|
|
41
|
+
imports: [
|
|
42
|
+
WorkflowModule.forRoot({
|
|
43
|
+
workflows: [orderWorkflow],
|
|
44
|
+
commands: [
|
|
45
|
+
{ name: "sendToWarehouse", useClass: SendToWarehouseCommand },
|
|
46
|
+
{ name: "notifyCustomer", useClass: NotifyCustomerCommand },
|
|
47
|
+
],
|
|
48
|
+
persistence: pgWorkflowProviders(pool),
|
|
49
|
+
enableControllers: true, // optional REST endpoints
|
|
50
|
+
}),
|
|
51
|
+
],
|
|
52
|
+
})
|
|
53
|
+
export class AppModule {}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Async Configuration
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
WorkflowModule.forRootAsync({
|
|
60
|
+
imports: [ConfigModule],
|
|
61
|
+
useFactory: (config: ConfigService) => ({
|
|
62
|
+
workflows: [orderWorkflow],
|
|
63
|
+
commands: [{ name: "sendToWarehouse", useClass: SendToWarehouseCommand }],
|
|
64
|
+
persistence: pgWorkflowProviders(new Pool({ connectionString: config.get("DATABASE_URL") })),
|
|
65
|
+
}),
|
|
66
|
+
inject: [ConfigService],
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Using WorkflowService
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { Injectable } from "@nestjs/common";
|
|
74
|
+
import { WorkflowService } from "@duraflows/nestjs";
|
|
75
|
+
|
|
76
|
+
@Injectable()
|
|
77
|
+
export class OrderService {
|
|
78
|
+
constructor(private readonly workflowService: WorkflowService) {}
|
|
79
|
+
|
|
80
|
+
async createOrder(orderData: CreateOrderDto) {
|
|
81
|
+
const instance = await this.workflowService.createInstance({
|
|
82
|
+
workflowName: "order",
|
|
83
|
+
metadata: { orderId: orderData.id },
|
|
84
|
+
});
|
|
85
|
+
return instance;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async receivePayment(instanceUuid: string, order: Order) {
|
|
89
|
+
const result = await this.workflowService.triggerEvent({
|
|
90
|
+
workflowInstanceUuid: instanceUuid,
|
|
91
|
+
eventName: "PaymentReceived",
|
|
92
|
+
subject: order,
|
|
93
|
+
});
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async getAvailableActions(instanceUuid: string) {
|
|
98
|
+
return this.workflowService.getAvailableEvents({ workflowInstanceUuid: instanceUuid });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### @WorkflowCommand Decorator
|
|
104
|
+
|
|
105
|
+
Mark command handlers for automatic discovery:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { WorkflowCommand as IWorkflowCommand, CommandResult, WorkflowExecutionContext } from "@duraflows/nestjs";
|
|
109
|
+
import { WorkflowCommand } from "@duraflows/nestjs";
|
|
110
|
+
|
|
111
|
+
@WorkflowCommand("sendToWarehouse")
|
|
112
|
+
export class SendToWarehouseCommand implements IWorkflowCommand {
|
|
113
|
+
constructor(private readonly warehouseClient: WarehouseClient) {} // NestJS DI works here
|
|
114
|
+
|
|
115
|
+
async execute(subject: unknown, ctx: WorkflowExecutionContext): Promise<CommandResult> {
|
|
116
|
+
await this.warehouseClient.ship(ctx.metadata.orderId as string);
|
|
117
|
+
return { ok: true, code: "SHIPPED" };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## REST Controllers
|
|
123
|
+
|
|
124
|
+
When `enableControllers: true` is set, the following endpoints are registered:
|
|
125
|
+
|
|
126
|
+
| Controller | Endpoints |
|
|
127
|
+
| --- | --- |
|
|
128
|
+
| `WorkflowInstanceController` | Create and retrieve workflow instances |
|
|
129
|
+
| `WorkflowEventController` | Trigger events on instances |
|
|
130
|
+
| `WorkflowQueryController` | Query available events and history |
|
|
131
|
+
| `WorkflowTimeoutController` | Process expired workflows |
|
|
132
|
+
|
|
133
|
+
## Documentation
|
|
134
|
+
|
|
135
|
+
See the full documentation in the [duraflows repository](https://github.com/camcima/duraflows).
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@duraflows/nestjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"build": "tsc --build"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@duraflows/core": "^0.0.
|
|
23
|
+
"@duraflows/core": "^0.0.2",
|
|
24
24
|
"@nestjs/common": "^11.0.0",
|
|
25
25
|
"@nestjs/core": "^11.0.0",
|
|
26
26
|
"class-transformer": "^0.5.1",
|