interaqt 0.8.15 → 1.0.0
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/agent/agentspace/knowledge/generator/api-reference.md +376 -80
- package/dist/builtins/index.d.ts +3 -0
- package/dist/builtins/index.d.ts.map +1 -0
- package/dist/builtins/init.d.ts +2 -0
- package/dist/builtins/init.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Action.d.ts +1 -1
- package/dist/builtins/interaction/Action.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Activity.d.ts +1 -1
- package/dist/builtins/interaction/Activity.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Attributive.d.ts +2 -2
- package/dist/builtins/interaction/Attributive.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Condition.d.ts +1 -1
- package/dist/builtins/interaction/Condition.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Conditions.d.ts +2 -2
- package/dist/builtins/interaction/Conditions.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Data.d.ts +1 -1
- package/dist/builtins/interaction/Data.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/DataAttributives.d.ts +2 -2
- package/dist/builtins/interaction/DataAttributives.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Event.d.ts +1 -1
- package/dist/builtins/interaction/Event.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Gateway.d.ts +1 -1
- package/dist/builtins/interaction/Gateway.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Interaction.d.ts +38 -14
- package/dist/builtins/interaction/Interaction.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/Payload.d.ts +1 -1
- package/dist/builtins/interaction/Payload.d.ts.map +1 -0
- package/dist/{shared → builtins/interaction}/PayloadItem.d.ts +2 -2
- package/dist/builtins/interaction/PayloadItem.d.ts.map +1 -0
- package/dist/builtins/interaction/User.d.ts.map +1 -0
- package/dist/{runtime → builtins/interaction}/activity/ActivityCall.d.ts +17 -13
- package/dist/builtins/interaction/activity/ActivityCall.d.ts.map +1 -0
- package/dist/{runtime → builtins/interaction}/activity/ActivityManager.d.ts +10 -5
- package/dist/builtins/interaction/activity/ActivityManager.d.ts.map +1 -0
- package/dist/{runtime → builtins/interaction}/activity/InteractionCall.d.ts +10 -27
- package/dist/builtins/interaction/activity/InteractionCall.d.ts.map +1 -0
- package/dist/{runtime → builtins/interaction}/errors/ActivityErrors.d.ts +1 -1
- package/dist/builtins/interaction/errors/ActivityErrors.d.ts.map +1 -0
- package/dist/{runtime → builtins/interaction}/errors/InteractionErrors.d.ts +1 -1
- package/dist/builtins/interaction/errors/InteractionErrors.d.ts.map +1 -0
- package/dist/builtins/interaction/index.d.ts +19 -0
- package/dist/builtins/interaction/index.d.ts.map +1 -0
- package/dist/core/Any.d.ts.map +1 -0
- package/dist/core/Average.d.ts.map +1 -0
- package/dist/core/BoolExp.d.ts.map +1 -0
- package/dist/{shared → core}/Computation.d.ts +2 -2
- package/dist/core/Computation.d.ts.map +1 -0
- package/dist/core/Count.d.ts.map +1 -0
- package/dist/core/Custom.d.ts.map +1 -0
- package/dist/core/Entity.d.ts.map +1 -0
- package/dist/core/EventSource.d.ts +52 -0
- package/dist/core/EventSource.d.ts.map +1 -0
- package/dist/core/Every.d.ts.map +1 -0
- package/dist/core/Property.d.ts.map +1 -0
- package/dist/core/RealDictionary.d.ts.map +1 -0
- package/dist/core/RealTime.d.ts.map +1 -0
- package/dist/core/RefContainer.d.ts.map +1 -0
- package/dist/core/Relation.d.ts.map +1 -0
- package/dist/core/SideEffect.d.ts.map +1 -0
- package/dist/core/StateMachine.d.ts.map +1 -0
- package/dist/core/StateNode.d.ts.map +1 -0
- package/dist/core/StateTransfer.d.ts.map +1 -0
- package/dist/core/Summation.d.ts.map +1 -0
- package/dist/core/Transform.d.ts.map +1 -0
- package/dist/core/WeightedSummation.d.ts.map +1 -0
- package/dist/{shared → core}/index.d.ts +8 -19
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/init.d.ts.map +1 -0
- package/dist/{shared → core}/interfaces.d.ts +0 -12
- package/dist/core/interfaces.d.ts.map +1 -0
- package/dist/{shared → core}/types.d.ts +5 -5
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/utils.d.ts.map +1 -0
- package/dist/drivers/Mysql.d.ts.map +1 -0
- package/dist/drivers/PGLite.d.ts.map +1 -0
- package/dist/drivers/PostgreSQL.d.ts.map +1 -0
- package/dist/drivers/SQLite.d.ts.map +1 -0
- package/dist/drivers/index.d.ts.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8574 -8394
- package/dist/index.js.map +1 -1
- package/dist/runtime/ComputationSourceMap.d.ts.map +1 -1
- package/dist/runtime/Controller.d.ts +32 -14
- package/dist/runtime/Controller.d.ts.map +1 -1
- package/dist/runtime/MonoSystem.d.ts +1 -1
- package/dist/runtime/MonoSystem.d.ts.map +1 -1
- package/dist/runtime/Scheduler.d.ts +1 -1
- package/dist/runtime/Scheduler.d.ts.map +1 -1
- package/dist/runtime/System.d.ts +1 -3
- package/dist/runtime/System.d.ts.map +1 -1
- package/dist/runtime/computations/Any.d.ts +1 -1
- package/dist/runtime/computations/Any.d.ts.map +1 -1
- package/dist/runtime/computations/Average.d.ts +1 -1
- package/dist/runtime/computations/Average.d.ts.map +1 -1
- package/dist/runtime/computations/Computation.d.ts +2 -2
- package/dist/runtime/computations/Computation.d.ts.map +1 -1
- package/dist/runtime/computations/Count.d.ts +6 -2
- package/dist/runtime/computations/Count.d.ts.map +1 -1
- package/dist/runtime/computations/Custom.d.ts +1 -1
- package/dist/runtime/computations/Custom.d.ts.map +1 -1
- package/dist/runtime/computations/Every.d.ts +1 -1
- package/dist/runtime/computations/Every.d.ts.map +1 -1
- package/dist/runtime/computations/RealTime.d.ts +1 -1
- package/dist/runtime/computations/RealTime.d.ts.map +1 -1
- package/dist/runtime/computations/StateMachine.d.ts +1 -1
- package/dist/runtime/computations/StateMachine.d.ts.map +1 -1
- package/dist/runtime/computations/Summation.d.ts +1 -1
- package/dist/runtime/computations/Summation.d.ts.map +1 -1
- package/dist/runtime/computations/Transform.d.ts +1 -1
- package/dist/runtime/computations/Transform.d.ts.map +1 -1
- package/dist/runtime/computations/TransitionFinder.d.ts +1 -1
- package/dist/runtime/computations/TransitionFinder.d.ts.map +1 -1
- package/dist/runtime/computations/WeightedSummation.d.ts +1 -1
- package/dist/runtime/computations/WeightedSummation.d.ts.map +1 -1
- package/dist/runtime/errors/ConditionErrors.d.ts +5 -5
- package/dist/runtime/errors/ConditionErrors.d.ts.map +1 -1
- package/dist/runtime/errors/index.d.ts +0 -2
- package/dist/runtime/errors/index.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +0 -3
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/storage/erstorage/MatchExp.d.ts +1 -1
- package/dist/storage/erstorage/MatchExp.d.ts.map +1 -1
- package/dist/storage/erstorage/MergedItemProcessor.d.ts +1 -1
- package/dist/storage/erstorage/MergedItemProcessor.d.ts.map +1 -1
- package/dist/storage/erstorage/SQLBuilder.d.ts +1 -1
- package/dist/storage/erstorage/SQLBuilder.d.ts.map +1 -1
- package/dist/storage/erstorage/Setup.d.ts +1 -1
- package/dist/storage/erstorage/Setup.d.ts.map +1 -1
- package/package.json +5 -5
- package/dist/dbclients/Mysql.d.ts.map +0 -1
- package/dist/dbclients/PGLite.d.ts.map +0 -1
- package/dist/dbclients/PostgreSQL.d.ts.map +0 -1
- package/dist/dbclients/SQLite.d.ts.map +0 -1
- package/dist/dbclients/index.d.ts.map +0 -1
- package/dist/runtime/activity/ActivityCall.d.ts.map +0 -1
- package/dist/runtime/activity/ActivityManager.d.ts.map +0 -1
- package/dist/runtime/activity/InteractionCall.d.ts.map +0 -1
- package/dist/runtime/errors/ActivityErrors.d.ts.map +0 -1
- package/dist/runtime/errors/InteractionErrors.d.ts.map +0 -1
- package/dist/shared/Action.d.ts.map +0 -1
- package/dist/shared/Activity.d.ts.map +0 -1
- package/dist/shared/Any.d.ts.map +0 -1
- package/dist/shared/Attributive.d.ts.map +0 -1
- package/dist/shared/Average.d.ts.map +0 -1
- package/dist/shared/BoolExp.d.ts.map +0 -1
- package/dist/shared/Computation.d.ts.map +0 -1
- package/dist/shared/Condition.d.ts.map +0 -1
- package/dist/shared/Conditions.d.ts.map +0 -1
- package/dist/shared/Count.d.ts.map +0 -1
- package/dist/shared/Custom.d.ts.map +0 -1
- package/dist/shared/Data.d.ts.map +0 -1
- package/dist/shared/DataAttributives.d.ts.map +0 -1
- package/dist/shared/Entity.d.ts.map +0 -1
- package/dist/shared/Event.d.ts.map +0 -1
- package/dist/shared/Every.d.ts.map +0 -1
- package/dist/shared/Gateway.d.ts.map +0 -1
- package/dist/shared/Interaction.d.ts.map +0 -1
- package/dist/shared/Payload.d.ts.map +0 -1
- package/dist/shared/PayloadItem.d.ts.map +0 -1
- package/dist/shared/Property.d.ts.map +0 -1
- package/dist/shared/RealDictionary.d.ts.map +0 -1
- package/dist/shared/RealTime.d.ts.map +0 -1
- package/dist/shared/RefContainer.d.ts.map +0 -1
- package/dist/shared/Relation.d.ts.map +0 -1
- package/dist/shared/SideEffect.d.ts.map +0 -1
- package/dist/shared/StateMachine.d.ts.map +0 -1
- package/dist/shared/StateNode.d.ts.map +0 -1
- package/dist/shared/StateTransfer.d.ts.map +0 -1
- package/dist/shared/Summation.d.ts.map +0 -1
- package/dist/shared/Transform.d.ts.map +0 -1
- package/dist/shared/User.d.ts.map +0 -1
- package/dist/shared/WeightedSummation.d.ts.map +0 -1
- package/dist/shared/index.d.ts.map +0 -1
- package/dist/shared/init.d.ts.map +0 -1
- package/dist/shared/interfaces.d.ts.map +0 -1
- package/dist/shared/types.d.ts.map +0 -1
- package/dist/shared/utils.d.ts.map +0 -1
- /package/dist/{shared → builtins/interaction}/User.d.ts +0 -0
- /package/dist/{shared → core}/Any.d.ts +0 -0
- /package/dist/{shared → core}/Average.d.ts +0 -0
- /package/dist/{shared → core}/BoolExp.d.ts +0 -0
- /package/dist/{shared → core}/Count.d.ts +0 -0
- /package/dist/{shared → core}/Custom.d.ts +0 -0
- /package/dist/{shared → core}/Entity.d.ts +0 -0
- /package/dist/{shared → core}/Every.d.ts +0 -0
- /package/dist/{shared → core}/Property.d.ts +0 -0
- /package/dist/{shared → core}/RealDictionary.d.ts +0 -0
- /package/dist/{shared → core}/RealTime.d.ts +0 -0
- /package/dist/{shared → core}/RefContainer.d.ts +0 -0
- /package/dist/{shared → core}/Relation.d.ts +0 -0
- /package/dist/{shared → core}/SideEffect.d.ts +0 -0
- /package/dist/{shared → core}/StateMachine.d.ts +0 -0
- /package/dist/{shared → core}/StateNode.d.ts +0 -0
- /package/dist/{shared → core}/StateTransfer.d.ts +0 -0
- /package/dist/{shared → core}/Summation.d.ts +0 -0
- /package/dist/{shared → core}/Transform.d.ts +0 -0
- /package/dist/{shared → core}/WeightedSummation.d.ts +0 -0
- /package/dist/{shared → core}/init.d.ts +0 -0
- /package/dist/{shared → core}/utils.d.ts +0 -0
- /package/dist/{dbclients → drivers}/Mysql.d.ts +0 -0
- /package/dist/{dbclients → drivers}/PGLite.d.ts +0 -0
- /package/dist/{dbclients → drivers}/PostgreSQL.d.ts +0 -0
- /package/dist/{dbclients → drivers}/SQLite.d.ts +0 -0
- /package/dist/{dbclients → drivers}/index.d.ts +0 -0
|
@@ -524,7 +524,151 @@ const UserProjectRelation = Relation.create({
|
|
|
524
524
|
|
|
525
525
|
**Note**: Merged relations cannot define their own `properties` - only `commonProperties` that specify the shared attribute contract. The `source`, `target`, and `type` are inherited from input relations (which must all have the same source/target).
|
|
526
526
|
|
|
527
|
-
## 13.2
|
|
527
|
+
## 13.2 EventSource API
|
|
528
|
+
|
|
529
|
+
### EventSource.create()
|
|
530
|
+
|
|
531
|
+
Create a custom event source. EventSource is the core abstraction for all event-driven data flows in interaqt. Interactions are a built-in EventSource type; you can create custom event sources for scheduled tasks, external system callbacks, or any other trigger.
|
|
532
|
+
|
|
533
|
+
**Syntax**
|
|
534
|
+
```typescript
|
|
535
|
+
EventSource.create<TArgs, TResult>(
|
|
536
|
+
config: EventSourceCreateArgs<TArgs, TResult>,
|
|
537
|
+
options?: { uuid?: string }
|
|
538
|
+
): EventSourceInstance<TArgs, TResult>
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
**Parameters**
|
|
542
|
+
- `config.name` (string, required): Event source name, used for identification and lookup
|
|
543
|
+
- `config.entity` (EntityInstance, required): The entity used to persist event records. Each dispatch creates a record in this entity.
|
|
544
|
+
- `config.guard` (function, optional): Validation function called before the event is processed. Throw an error to reject the event.
|
|
545
|
+
```typescript
|
|
546
|
+
async function(this: Controller, args: TArgs): Promise<void>
|
|
547
|
+
```
|
|
548
|
+
- `config.mapEventData` (function, optional): Maps event args to the data stored in the event entity record.
|
|
549
|
+
```typescript
|
|
550
|
+
function(args: TArgs): Record<string, any>
|
|
551
|
+
```
|
|
552
|
+
- `config.resolve` (function, optional): Resolves and returns data after the event is processed (e.g., for query events).
|
|
553
|
+
```typescript
|
|
554
|
+
async function(this: Controller, args: TArgs): Promise<TResult>
|
|
555
|
+
```
|
|
556
|
+
- `config.afterDispatch` (function, optional): Hook called after dispatch completes. Can return additional context.
|
|
557
|
+
```typescript
|
|
558
|
+
async function(this: Controller, args: TArgs, result: { data?: TResult }): Promise<Record<string, unknown> | void>
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
**EventSourceInstance Interface**
|
|
562
|
+
```typescript
|
|
563
|
+
interface EventSourceInstance<TArgs = any, TResult = void> {
|
|
564
|
+
uuid: string
|
|
565
|
+
_type: string
|
|
566
|
+
name: string
|
|
567
|
+
entity: EntityInstance
|
|
568
|
+
guard?: (this: Controller, args: TArgs) => Promise<void>
|
|
569
|
+
mapEventData?: (args: TArgs) => Record<string, any>
|
|
570
|
+
resolve?: (this: Controller, args: TArgs) => Promise<TResult>
|
|
571
|
+
afterDispatch?: (this: Controller, args: TArgs, result: { data?: TResult }) => Promise<Record<string, unknown> | void>
|
|
572
|
+
}
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
**Examples**
|
|
576
|
+
|
|
577
|
+
```typescript
|
|
578
|
+
// Define event entity for storing event records
|
|
579
|
+
const ScheduledTaskEvent = Entity.create({
|
|
580
|
+
name: '_ScheduledTask_',
|
|
581
|
+
properties: [
|
|
582
|
+
Property.create({ name: 'taskName', type: 'string' }),
|
|
583
|
+
Property.create({ name: 'triggeredAt', type: 'number' }),
|
|
584
|
+
Property.create({ name: 'payload', type: 'object' })
|
|
585
|
+
]
|
|
586
|
+
})
|
|
587
|
+
|
|
588
|
+
// Create custom event source for scheduled tasks
|
|
589
|
+
const scheduledTaskSource = EventSource.create({
|
|
590
|
+
name: 'scheduledTask',
|
|
591
|
+
entity: ScheduledTaskEvent,
|
|
592
|
+
guard: async function(this: Controller, args: { taskName: string, payload: any }) {
|
|
593
|
+
if (!args.taskName) {
|
|
594
|
+
throw new Error('Task name is required')
|
|
595
|
+
}
|
|
596
|
+
},
|
|
597
|
+
mapEventData: (args) => ({
|
|
598
|
+
taskName: args.taskName,
|
|
599
|
+
triggeredAt: Math.floor(Date.now() / 1000),
|
|
600
|
+
payload: args.payload
|
|
601
|
+
})
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
// Register and dispatch
|
|
605
|
+
const controller = new Controller({
|
|
606
|
+
system,
|
|
607
|
+
entities: [User, Post],
|
|
608
|
+
relations: [],
|
|
609
|
+
eventSources: [scheduledTaskSource],
|
|
610
|
+
dict: []
|
|
611
|
+
})
|
|
612
|
+
|
|
613
|
+
await controller.setup(true)
|
|
614
|
+
|
|
615
|
+
// Dispatch the event
|
|
616
|
+
const result = await controller.dispatch(scheduledTaskSource, {
|
|
617
|
+
taskName: 'cleanupExpiredSessions',
|
|
618
|
+
payload: { maxAge: 86400 }
|
|
619
|
+
})
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
**Custom Event Source for External Callbacks**
|
|
623
|
+
|
|
624
|
+
```typescript
|
|
625
|
+
const WebhookEvent = Entity.create({
|
|
626
|
+
name: '_Webhook_',
|
|
627
|
+
properties: [
|
|
628
|
+
Property.create({ name: 'source', type: 'string' }),
|
|
629
|
+
Property.create({ name: 'eventType', type: 'string' }),
|
|
630
|
+
Property.create({ name: 'data', type: 'object' })
|
|
631
|
+
]
|
|
632
|
+
})
|
|
633
|
+
|
|
634
|
+
const webhookSource = EventSource.create({
|
|
635
|
+
name: 'webhook',
|
|
636
|
+
entity: WebhookEvent,
|
|
637
|
+
guard: async function(this: Controller, args: { source: string, secret: string, eventType: string, data: any }) {
|
|
638
|
+
const validSecret = await this.system.storage.get('config', `webhook_secret_${args.source}`)
|
|
639
|
+
if (args.secret !== validSecret) {
|
|
640
|
+
throw new Error('Invalid webhook secret')
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
mapEventData: (args) => ({
|
|
644
|
+
source: args.source,
|
|
645
|
+
eventType: args.eventType,
|
|
646
|
+
data: args.data
|
|
647
|
+
}),
|
|
648
|
+
resolve: async function(this: Controller, args) {
|
|
649
|
+
return { received: true, timestamp: Date.now() }
|
|
650
|
+
}
|
|
651
|
+
})
|
|
652
|
+
|
|
653
|
+
// Dispatch when webhook is received
|
|
654
|
+
const result = await controller.dispatch(webhookSource, {
|
|
655
|
+
source: 'stripe',
|
|
656
|
+
secret: 'whsec_xxx',
|
|
657
|
+
eventType: 'payment.completed',
|
|
658
|
+
data: { orderId: 'order_123', amount: 9900 }
|
|
659
|
+
})
|
|
660
|
+
```
|
|
661
|
+
|
|
662
|
+
**Key Points**
|
|
663
|
+
1. Every event source must have an `entity` to persist event records
|
|
664
|
+
2. `guard` runs before event processing; throw to reject the event
|
|
665
|
+
3. `mapEventData` converts dispatch args into the format stored in the entity
|
|
666
|
+
4. `resolve` returns data to the caller (useful for query-type events)
|
|
667
|
+
5. `afterDispatch` runs after successful processing and can return additional context
|
|
668
|
+
6. The Controller automatically registers the event source's entity if not already in the entities list
|
|
669
|
+
7. Interaction is a built-in EventSource type that provides pre-built `guard`, `mapEventData`, and `resolve` implementations
|
|
670
|
+
|
|
671
|
+
## 13.3 Computation-Related APIs
|
|
528
672
|
|
|
529
673
|
### Count.create()
|
|
530
674
|
|
|
@@ -1891,17 +2035,16 @@ const activeUsers = Dictionary.create({
|
|
|
1891
2035
|
|
|
1892
2036
|
**Usage in Controller**
|
|
1893
2037
|
|
|
1894
|
-
Dictionaries are passed
|
|
2038
|
+
Dictionaries are passed via the `dict` parameter to Controller:
|
|
1895
2039
|
|
|
1896
2040
|
```typescript
|
|
1897
2041
|
const controller = new Controller({
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
recordMutationSideEffects: []
|
|
2042
|
+
system: system,
|
|
2043
|
+
entities: entities,
|
|
2044
|
+
relations: relations,
|
|
2045
|
+
eventSources: [...interactions],
|
|
2046
|
+
dict: [userCountDict, systemConfig, currentTime, activeUsers],
|
|
2047
|
+
recordMutationSideEffects: []
|
|
1905
2048
|
});
|
|
1906
2049
|
```
|
|
1907
2050
|
|
|
@@ -2306,11 +2449,13 @@ const bulkApproveTransfer = StateTransfer.create({
|
|
|
2306
2449
|
});
|
|
2307
2450
|
```
|
|
2308
2451
|
|
|
2309
|
-
## 13.
|
|
2452
|
+
## 13.4 Interaction-Related APIs
|
|
2453
|
+
|
|
2454
|
+
Interaction is a built-in EventSource type provided by interaqt. It implements `EventSourceInstance<InteractionEventArgs>` and provides pre-built guard (condition checks, user validation, payload validation), event data mapping, and data retrieval logic.
|
|
2310
2455
|
|
|
2311
2456
|
### Interaction.create()
|
|
2312
2457
|
|
|
2313
|
-
Create user interaction definition.
|
|
2458
|
+
Create user interaction definition. Returns an `InteractionInstance` which implements `EventSourceInstance<InteractionEventArgs>` and can be directly passed to `controller.dispatch()`.
|
|
2314
2459
|
|
|
2315
2460
|
**Syntax**
|
|
2316
2461
|
```typescript
|
|
@@ -2322,7 +2467,6 @@ Interaction.create(config: InteractionConfig): InteractionInstance
|
|
|
2322
2467
|
- `config.action` (Action, required): Interaction action
|
|
2323
2468
|
- `config.payload` (Payload, optional): Interaction parameters
|
|
2324
2469
|
- `config.conditions` (Condition|Conditions, optional): Execution conditions
|
|
2325
|
-
- `config.sideEffects` (SideEffect[], optional): Side effect handlers
|
|
2326
2470
|
- `config.data` (Entity|Relation, optional): Associated data entity
|
|
2327
2471
|
- `config.dataPolicy` (DataPolicy, optional): Data access policy for data fetching interactions
|
|
2328
2472
|
|
|
@@ -2410,13 +2554,13 @@ const GetPostsPaginated = Interaction.create({
|
|
|
2410
2554
|
|
|
2411
2555
|
```typescript
|
|
2412
2556
|
// Get all users
|
|
2413
|
-
const result = await controller.
|
|
2557
|
+
const result = await controller.dispatch(GetAllUsers, {
|
|
2414
2558
|
user: { id: 'admin-user' }
|
|
2415
2559
|
})
|
|
2416
2560
|
// result.data contains array of users
|
|
2417
2561
|
|
|
2418
2562
|
// Get filtered users with runtime query
|
|
2419
|
-
const activeUsersResult = await controller.
|
|
2563
|
+
const activeUsersResult = await controller.dispatch(GetActiveUsers, {
|
|
2420
2564
|
user: { id: 'admin-user' },
|
|
2421
2565
|
query: {
|
|
2422
2566
|
match: MatchExp.atom({ key: 'status', value: ['=', 'active'] }),
|
|
@@ -2426,7 +2570,7 @@ const activeUsersResult = await controller.callInteraction('getActiveUsers', {
|
|
|
2426
2570
|
// result.data contains filtered users with specified fields
|
|
2427
2571
|
|
|
2428
2572
|
// Get user's posts
|
|
2429
|
-
const userPostsResult = await controller.
|
|
2573
|
+
const userPostsResult = await controller.dispatch(GetUserPosts, {
|
|
2430
2574
|
user: { id: 'user123' },
|
|
2431
2575
|
query: {
|
|
2432
2576
|
match: MatchExp.atom({ key: 'author.id', value: ['=', 'user123'] }),
|
|
@@ -2436,7 +2580,7 @@ const userPostsResult = await controller.callInteraction('getUserPosts', {
|
|
|
2436
2580
|
// result.data contains posts authored by user123
|
|
2437
2581
|
|
|
2438
2582
|
// Get paginated posts
|
|
2439
|
-
const paginatedResult = await controller.
|
|
2583
|
+
const paginatedResult = await controller.dispatch(GetPostsPaginated, {
|
|
2440
2584
|
user: { id: 'user123' },
|
|
2441
2585
|
query: {
|
|
2442
2586
|
match: MatchExp.atom({ key: 'status', value: ['=', 'published'] }),
|
|
@@ -2452,7 +2596,7 @@ const paginatedResult = await controller.callInteraction('getPostsPaginated', {
|
|
|
2452
2596
|
1. **Required**: Must use `GetAction` as the action
|
|
2453
2597
|
2. **Required**: Must specify `data` field with the Entity or Relation to query
|
|
2454
2598
|
3. **Optional**: Use `dataPolicy` in Interaction definition to set fixed data access policies (match constraints, field restrictions, default sorting/pagination)
|
|
2455
|
-
4. **Runtime Query**: Pass `query` object in `
|
|
2599
|
+
4. **Runtime Query**: Pass `query` object in `controller.dispatch()` to dynamically filter data at runtime (user-provided filters)
|
|
2456
2600
|
5. **Conditions**: Can use conditions to control access based on query parameters
|
|
2457
2601
|
6. **Response**: Retrieved data is returned in `result.data` field
|
|
2458
2602
|
|
|
@@ -2659,7 +2803,7 @@ const GetProducts = Interaction.create({
|
|
|
2659
2803
|
})
|
|
2660
2804
|
|
|
2661
2805
|
// User calls with additional filters
|
|
2662
|
-
const result = await controller.
|
|
2806
|
+
const result = await controller.dispatch(GetProducts, {
|
|
2663
2807
|
user: { id: 'user123' },
|
|
2664
2808
|
query: {
|
|
2665
2809
|
match: MatchExp.atom({ key: 'category', value: ['=', 'electronics'] }),
|
|
@@ -2747,7 +2891,7 @@ const activityItem = PayloadItem.create({
|
|
|
2747
2891
|
})
|
|
2748
2892
|
```
|
|
2749
2893
|
|
|
2750
|
-
## 13.
|
|
2894
|
+
## 13.5 Activity-Related APIs
|
|
2751
2895
|
|
|
2752
2896
|
### Activity.create()
|
|
2753
2897
|
|
|
@@ -2979,7 +3123,7 @@ conditions: Conditions.create({
|
|
|
2979
3123
|
|
|
2980
3124
|
**Error Handling**
|
|
2981
3125
|
|
|
2982
|
-
When a condition fails or throws an error, the
|
|
3126
|
+
When a condition fails or throws an error, the dispatch returns:
|
|
2983
3127
|
```typescript
|
|
2984
3128
|
{
|
|
2985
3129
|
error: {
|
|
@@ -2989,31 +3133,64 @@ When a condition fails or throws an error, the interaction call returns:
|
|
|
2989
3133
|
}
|
|
2990
3134
|
```
|
|
2991
3135
|
|
|
2992
|
-
## 13.
|
|
3136
|
+
## 13.6 System-Related APIs
|
|
2993
3137
|
|
|
2994
3138
|
### Controller
|
|
2995
3139
|
|
|
2996
|
-
System controller that coordinates the work of various components.
|
|
3140
|
+
System controller that coordinates the work of various components. All events flow through the Controller via the `dispatch()` API.
|
|
2997
3141
|
|
|
2998
3142
|
**Constructor**
|
|
2999
3143
|
```typescript
|
|
3000
3144
|
new Controller({
|
|
3001
3145
|
system: System,
|
|
3002
|
-
entities
|
|
3003
|
-
relations
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
dict?: DictionaryInstance[], // Note: This is for global dictionaries, NOT computations
|
|
3146
|
+
entities?: EntityInstance[],
|
|
3147
|
+
relations?: RelationInstance[],
|
|
3148
|
+
eventSources?: EventSourceInstance<any, any>[],
|
|
3149
|
+
dict?: DictionaryInstance[],
|
|
3007
3150
|
recordMutationSideEffects?: RecordMutationSideEffect[],
|
|
3008
|
-
|
|
3151
|
+
computations?: (new (...args: any[]) => Computation)[],
|
|
3152
|
+
ignoreGuard?: boolean, // Skip guard checks when true
|
|
3153
|
+
forceThrowDispatchError?: boolean // Throw errors instead of returning them
|
|
3009
3154
|
})
|
|
3010
3155
|
```
|
|
3011
3156
|
|
|
3012
|
-
|
|
3157
|
+
**Parameters**
|
|
3158
|
+
- `system` (System, required): System implementation providing storage and logging
|
|
3159
|
+
- `entities` (EntityInstance[], optional): Entity definitions
|
|
3160
|
+
- `relations` (RelationInstance[], optional): Relation definitions
|
|
3161
|
+
- `eventSources` (EventSourceInstance[], optional): All event sources (Interactions, custom EventSources, etc.)
|
|
3162
|
+
- `dict` (DictionaryInstance[], optional): Global dictionary definitions
|
|
3163
|
+
- `recordMutationSideEffects` (RecordMutationSideEffect[], optional): Side effects triggered on record mutations
|
|
3164
|
+
- `computations` ((new (...args: any[]) => Computation)[], optional): Additional computation handle classes
|
|
3165
|
+
- `ignoreGuard` (boolean, optional): When true, skips all guard checks on event sources
|
|
3166
|
+
- `forceThrowDispatchError` (boolean, optional): When true, throws errors instead of returning them in the response
|
|
3167
|
+
|
|
3168
|
+
**Note**: The Controller automatically registers event source entities (e.g., `InteractionEventEntity`) if they are not already in the entities list. You do not need to manually add them.
|
|
3169
|
+
|
|
3170
|
+
**Example**
|
|
3171
|
+
```typescript
|
|
3172
|
+
const controller = new Controller({
|
|
3173
|
+
system,
|
|
3174
|
+
entities: [User, Post],
|
|
3175
|
+
relations: [UserPostRelation],
|
|
3176
|
+
eventSources: [
|
|
3177
|
+
CreatePostInteraction,
|
|
3178
|
+
LikePostInteraction,
|
|
3179
|
+
GetAllPostsInteraction,
|
|
3180
|
+
scheduledCleanupSource // Custom EventSource
|
|
3181
|
+
],
|
|
3182
|
+
dict: [userCountDict, systemConfig],
|
|
3183
|
+
recordMutationSideEffects: [auditLogger]
|
|
3184
|
+
})
|
|
3185
|
+
|
|
3186
|
+
await controller.setup(true)
|
|
3187
|
+
```
|
|
3188
|
+
|
|
3189
|
+
⚠️ **IMPORTANT**: Controller does NOT accept a computations parameter for defining reactive computations. All reactive computations should be defined within the `computation` field of Entity/Relation/Property/Dictionary definitions. The `computations` parameter is only for registering additional computation handle classes.
|
|
3013
3190
|
|
|
3014
3191
|
### RecordMutationSideEffect
|
|
3015
3192
|
|
|
3016
|
-
RecordMutationSideEffect allows you to execute custom logic when records are created, updated, or deleted within
|
|
3193
|
+
RecordMutationSideEffect allows you to execute custom logic when records are created, updated, or deleted within dispatch contexts. It's useful for triggering external operations, logging, or custom business logic in response to data changes.
|
|
3017
3194
|
|
|
3018
3195
|
**Syntax**
|
|
3019
3196
|
```typescript
|
|
@@ -3041,11 +3218,11 @@ type RecordMutationEvent = {
|
|
|
3041
3218
|
```
|
|
3042
3219
|
|
|
3043
3220
|
**Important Notes**
|
|
3044
|
-
- RecordMutationSideEffect **only** triggers within
|
|
3221
|
+
- RecordMutationSideEffect **only** triggers within dispatch execution context
|
|
3045
3222
|
- Direct storage operations (e.g., `controller.system.storage.create()`) do **NOT** trigger side effects
|
|
3046
3223
|
- All side effects registered for an entity are called on any mutation of that entity
|
|
3047
3224
|
- Side effects run after the storage operation completes
|
|
3048
|
-
- Side effect errors are captured but don't fail the
|
|
3225
|
+
- Side effect errors are captured but don't fail the dispatch
|
|
3049
3226
|
|
|
3050
3227
|
**Examples**
|
|
3051
3228
|
|
|
@@ -3105,7 +3282,7 @@ const controller = new Controller({
|
|
|
3105
3282
|
system: system,
|
|
3106
3283
|
entities: [User, Order, Product],
|
|
3107
3284
|
relations: [],
|
|
3108
|
-
|
|
3285
|
+
eventSources: [CreateUserInteraction, UpdateOrderInteraction],
|
|
3109
3286
|
recordMutationSideEffects: [
|
|
3110
3287
|
userCreatedLogger,
|
|
3111
3288
|
auditLogger,
|
|
@@ -3113,8 +3290,8 @@ const controller = new Controller({
|
|
|
3113
3290
|
]
|
|
3114
3291
|
});
|
|
3115
3292
|
|
|
3116
|
-
// When
|
|
3117
|
-
const result = await controller.
|
|
3293
|
+
// When event is dispatched, side effects are triggered
|
|
3294
|
+
const result = await controller.dispatch(CreateUserInteraction, {
|
|
3118
3295
|
user: { id: 'admin' },
|
|
3119
3296
|
payload: { name: 'John', email: 'john@example.com' }
|
|
3120
3297
|
});
|
|
@@ -3150,33 +3327,36 @@ Initialize system.
|
|
|
3150
3327
|
await controller.setup(true) // Create database tables
|
|
3151
3328
|
```
|
|
3152
3329
|
|
|
3153
|
-
####
|
|
3154
|
-
|
|
3330
|
+
#### dispatch(eventSource, args)
|
|
3331
|
+
Unified dispatch API for all event source types. This is the primary way to trigger events in the system.
|
|
3155
3332
|
|
|
3156
|
-
**
|
|
3333
|
+
**Syntax**
|
|
3334
|
+
```typescript
|
|
3335
|
+
async dispatch<TArgs, TResult>(
|
|
3336
|
+
eventSource: EventSourceInstance<TArgs, TResult>,
|
|
3337
|
+
args: TArgs
|
|
3338
|
+
): Promise<DispatchResponse>
|
|
3339
|
+
```
|
|
3157
3340
|
|
|
3158
3341
|
**Parameters**
|
|
3159
|
-
- `
|
|
3160
|
-
- `args
|
|
3161
|
-
|
|
3162
|
-
|
|
3342
|
+
- `eventSource` (EventSourceInstance, required): The event source instance to dispatch (object reference, not a name string)
|
|
3343
|
+
- `args` (TArgs, required): Event-specific arguments. For Interactions, this is `InteractionEventArgs` containing `user`, `payload`, and optional `query`.
|
|
3344
|
+
|
|
3345
|
+
**Note about ignoreGuard**: When `controller.ignoreGuard` is set to `true`, dispatch will bypass all guard checks (conditions, user validation, payload validation) defined in the event source.
|
|
3163
3346
|
|
|
3164
3347
|
**Return Type**
|
|
3165
3348
|
```typescript
|
|
3166
|
-
type
|
|
3167
|
-
// Contains error information if the
|
|
3349
|
+
type DispatchResponse = {
|
|
3350
|
+
// Contains error information if the dispatch failed
|
|
3168
3351
|
error?: unknown
|
|
3169
3352
|
|
|
3170
|
-
// For
|
|
3353
|
+
// For events with resolve: contains the resolved data
|
|
3171
3354
|
data?: unknown
|
|
3172
3355
|
|
|
3173
|
-
// The interaction event that was processed
|
|
3174
|
-
event?: InteractionEvent
|
|
3175
|
-
|
|
3176
3356
|
// Record mutations (create/update/delete) that occurred
|
|
3177
3357
|
effects?: RecordMutationEvent[]
|
|
3178
3358
|
|
|
3179
|
-
// Results from
|
|
3359
|
+
// Results from registered RecordMutationSideEffects
|
|
3180
3360
|
sideEffects?: {
|
|
3181
3361
|
[effectName: string]: {
|
|
3182
3362
|
result?: unknown
|
|
@@ -3184,23 +3364,32 @@ type InteractionCallResponse = {
|
|
|
3184
3364
|
}
|
|
3185
3365
|
}
|
|
3186
3366
|
|
|
3187
|
-
// Additional context
|
|
3367
|
+
// Additional context from afterDispatch hook
|
|
3188
3368
|
context?: {
|
|
3189
3369
|
[key: string]: unknown
|
|
3190
3370
|
}
|
|
3191
3371
|
}
|
|
3192
3372
|
```
|
|
3193
3373
|
|
|
3194
|
-
**
|
|
3374
|
+
**Dispatch Flow**
|
|
3375
|
+
1. Begin transaction
|
|
3376
|
+
2. Execute `guard` (if provided and `ignoreGuard` is false)
|
|
3377
|
+
3. Map event data via `mapEventData` and create event record
|
|
3378
|
+
4. Execute `resolve` (if provided) - returns data
|
|
3379
|
+
5. Execute `afterDispatch` (if provided) - returns context
|
|
3380
|
+
6. Commit transaction
|
|
3381
|
+
7. Run RecordMutationSideEffects
|
|
3382
|
+
|
|
3383
|
+
**Example - Dispatch Interaction**
|
|
3195
3384
|
```typescript
|
|
3196
|
-
const result = await controller.
|
|
3385
|
+
const result = await controller.dispatch(CreatePostInteraction, {
|
|
3197
3386
|
user: { id: 'user1' },
|
|
3198
3387
|
payload: { postData: { title: 'Hello', content: 'World' } }
|
|
3199
3388
|
})
|
|
3200
3389
|
|
|
3201
3390
|
// Check for errors
|
|
3202
3391
|
if (result.error) {
|
|
3203
|
-
console.error('
|
|
3392
|
+
console.error('Dispatch failed:', result.error)
|
|
3204
3393
|
return
|
|
3205
3394
|
}
|
|
3206
3395
|
|
|
@@ -3213,14 +3402,40 @@ if (result.sideEffects?.emailNotification?.error) {
|
|
|
3213
3402
|
}
|
|
3214
3403
|
```
|
|
3215
3404
|
|
|
3216
|
-
**Example -
|
|
3405
|
+
**Example - Dispatch Custom EventSource**
|
|
3217
3406
|
```typescript
|
|
3218
|
-
const result = await controller.
|
|
3219
|
-
'
|
|
3220
|
-
|
|
3221
|
-
|
|
3222
|
-
|
|
3223
|
-
|
|
3407
|
+
const result = await controller.dispatch(scheduledTaskSource, {
|
|
3408
|
+
taskName: 'dailyReport',
|
|
3409
|
+
payload: { date: '2024-01-15' }
|
|
3410
|
+
})
|
|
3411
|
+
```
|
|
3412
|
+
|
|
3413
|
+
**Example - Dispatch Get Interaction**
|
|
3414
|
+
```typescript
|
|
3415
|
+
const result = await controller.dispatch(GetAllPostsInteraction, {
|
|
3416
|
+
user: { id: 'user1' },
|
|
3417
|
+
query: {
|
|
3418
|
+
match: MatchExp.atom({ key: 'status', value: ['=', 'published'] }),
|
|
3419
|
+
attributeQuery: ['id', 'title', 'content', 'createdAt']
|
|
3420
|
+
}
|
|
3421
|
+
})
|
|
3422
|
+
// result.data contains the queried records
|
|
3423
|
+
```
|
|
3424
|
+
|
|
3425
|
+
#### findEventSourceByName(name)
|
|
3426
|
+
Find a registered event source by its name.
|
|
3427
|
+
|
|
3428
|
+
**Syntax**
|
|
3429
|
+
```typescript
|
|
3430
|
+
findEventSourceByName(name: string): EventSourceInstance | undefined
|
|
3431
|
+
```
|
|
3432
|
+
|
|
3433
|
+
**Example**
|
|
3434
|
+
```typescript
|
|
3435
|
+
const source = controller.findEventSourceByName('createPost')
|
|
3436
|
+
if (source) {
|
|
3437
|
+
await controller.dispatch(source, { user: { id: 'user1' }, payload: {...} })
|
|
3438
|
+
}
|
|
3224
3439
|
```
|
|
3225
3440
|
|
|
3226
3441
|
### System
|
|
@@ -3838,7 +4053,7 @@ type ModifierData = {
|
|
|
3838
4053
|
}
|
|
3839
4054
|
```
|
|
3840
4055
|
|
|
3841
|
-
## 13.
|
|
4056
|
+
## 13.7 Utility Function APIs
|
|
3842
4057
|
|
|
3843
4058
|
### MatchExp
|
|
3844
4059
|
|
|
@@ -3924,7 +4139,7 @@ const expr2 = BoolExp.atom({ condition: 'isAdmin' })
|
|
|
3924
4139
|
const combined = expr1.and(expr2).or({ condition: 'isOwner' })
|
|
3925
4140
|
```
|
|
3926
4141
|
|
|
3927
|
-
## 13.
|
|
4142
|
+
## 13.8 Query APIs
|
|
3928
4143
|
|
|
3929
4144
|
### Conditions
|
|
3930
4145
|
|
|
@@ -3988,11 +4203,37 @@ const combined = expr1.and(expr2).or({ condition: 'isOwner' })
|
|
|
3988
4203
|
### Core Types
|
|
3989
4204
|
|
|
3990
4205
|
```typescript
|
|
3991
|
-
//
|
|
4206
|
+
// EventSource instance interface - the core abstraction for all event sources
|
|
4207
|
+
interface EventSourceInstance<TArgs = any, TResult = void> {
|
|
4208
|
+
uuid: string
|
|
4209
|
+
_type: string
|
|
4210
|
+
name: string
|
|
4211
|
+
entity: EntityInstance
|
|
4212
|
+
guard?: (this: Controller, args: TArgs) => Promise<void>
|
|
4213
|
+
mapEventData?: (args: TArgs) => Record<string, any>
|
|
4214
|
+
resolve?: (this: Controller, args: TArgs) => Promise<TResult>
|
|
4215
|
+
afterDispatch?: (this: Controller, args: TArgs, result: { data?: TResult }) => Promise<Record<string, unknown> | void>
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4218
|
+
// Dispatch response - returned by controller.dispatch()
|
|
4219
|
+
type DispatchResponse = {
|
|
4220
|
+
error?: unknown
|
|
4221
|
+
data?: unknown
|
|
4222
|
+
effects?: RecordMutationEvent[]
|
|
4223
|
+
sideEffects?: { [k: string]: { result?: unknown, error?: unknown } }
|
|
4224
|
+
context?: { [k: string]: unknown }
|
|
4225
|
+
}
|
|
4226
|
+
|
|
4227
|
+
// Interaction event arguments (used when dispatching Interactions)
|
|
3992
4228
|
type InteractionEventArgs = {
|
|
3993
4229
|
user: { id: string, [key: string]: any }
|
|
3994
4230
|
payload?: { [key: string]: any }
|
|
3995
|
-
|
|
4231
|
+
query?: {
|
|
4232
|
+
match?: any
|
|
4233
|
+
modifier?: Record<string, unknown>
|
|
4234
|
+
attributeQuery?: string[]
|
|
4235
|
+
}
|
|
4236
|
+
activityId?: string
|
|
3996
4237
|
}
|
|
3997
4238
|
|
|
3998
4239
|
// Record mutation event
|
|
@@ -4048,7 +4289,12 @@ type ComputationResultPatch = {
|
|
|
4048
4289
|
### Complete Blog System Example
|
|
4049
4290
|
|
|
4050
4291
|
```typescript
|
|
4051
|
-
import {
|
|
4292
|
+
import {
|
|
4293
|
+
Entity, Property, Relation, Controller,
|
|
4294
|
+
EventSource, Count, Transform, InteractionEventEntity,
|
|
4295
|
+
Action, Payload, PayloadItem, Condition, GetAction, MatchExp
|
|
4296
|
+
} from 'interaqt'
|
|
4297
|
+
import { Interaction } from 'interaqt/builtins'
|
|
4052
4298
|
|
|
4053
4299
|
// 1. Define entities
|
|
4054
4300
|
const User = Entity.create({
|
|
@@ -4059,7 +4305,7 @@ const User = Entity.create({
|
|
|
4059
4305
|
Property.create({
|
|
4060
4306
|
name: 'postCount',
|
|
4061
4307
|
type: 'number',
|
|
4062
|
-
computation: Count.create({ property: 'posts' })
|
|
4308
|
+
computation: Count.create({ property: 'posts' })
|
|
4063
4309
|
})
|
|
4064
4310
|
]
|
|
4065
4311
|
})
|
|
@@ -4072,9 +4318,27 @@ const Post = Entity.create({
|
|
|
4072
4318
|
Property.create({
|
|
4073
4319
|
name: 'likeCount',
|
|
4074
4320
|
type: 'number',
|
|
4075
|
-
computation: Count.create({ property: 'likes' })
|
|
4321
|
+
computation: Count.create({ property: 'likes' })
|
|
4076
4322
|
})
|
|
4077
|
-
]
|
|
4323
|
+
],
|
|
4324
|
+
computation: Transform.create({
|
|
4325
|
+
eventDeps: {
|
|
4326
|
+
PostInteraction: {
|
|
4327
|
+
recordName: InteractionEventEntity.name,
|
|
4328
|
+
type: 'create'
|
|
4329
|
+
}
|
|
4330
|
+
},
|
|
4331
|
+
callback: async function(this: Controller, mutationEvent) {
|
|
4332
|
+
const event = mutationEvent.record
|
|
4333
|
+
if (event.interactionName === 'createPost') {
|
|
4334
|
+
return {
|
|
4335
|
+
title: event.payload.postData.title,
|
|
4336
|
+
content: event.payload.postData.content
|
|
4337
|
+
}
|
|
4338
|
+
}
|
|
4339
|
+
return null
|
|
4340
|
+
}
|
|
4341
|
+
})
|
|
4078
4342
|
})
|
|
4079
4343
|
|
|
4080
4344
|
// 2. Define relations
|
|
@@ -4083,7 +4347,25 @@ const UserPostRelation = Relation.create({
|
|
|
4083
4347
|
sourceProperty: 'posts',
|
|
4084
4348
|
target: Post,
|
|
4085
4349
|
targetProperty: 'author',
|
|
4086
|
-
type: '1:n'
|
|
4350
|
+
type: '1:n',
|
|
4351
|
+
computation: Transform.create({
|
|
4352
|
+
eventDeps: {
|
|
4353
|
+
PostInteraction: {
|
|
4354
|
+
recordName: InteractionEventEntity.name,
|
|
4355
|
+
type: 'create'
|
|
4356
|
+
}
|
|
4357
|
+
},
|
|
4358
|
+
callback: async function(this: Controller, mutationEvent) {
|
|
4359
|
+
const event = mutationEvent.record
|
|
4360
|
+
if (event.interactionName === 'createPost') {
|
|
4361
|
+
return {
|
|
4362
|
+
source: event.user,
|
|
4363
|
+
target: event.payload.postData
|
|
4364
|
+
}
|
|
4365
|
+
}
|
|
4366
|
+
return null
|
|
4367
|
+
}
|
|
4368
|
+
})
|
|
4087
4369
|
})
|
|
4088
4370
|
|
|
4089
4371
|
const PostLikeRelation = Relation.create({
|
|
@@ -4094,7 +4376,7 @@ const PostLikeRelation = Relation.create({
|
|
|
4094
4376
|
type: 'n:n'
|
|
4095
4377
|
})
|
|
4096
4378
|
|
|
4097
|
-
// 3. Define interactions
|
|
4379
|
+
// 3. Define interactions (built-in EventSource type)
|
|
4098
4380
|
const CreatePostInteraction = Interaction.create({
|
|
4099
4381
|
name: 'createPost',
|
|
4100
4382
|
action: Action.create({ name: 'create' }),
|
|
@@ -4128,20 +4410,25 @@ const LikePostInteraction = Interaction.create({
|
|
|
4128
4410
|
})
|
|
4129
4411
|
})
|
|
4130
4412
|
|
|
4131
|
-
|
|
4413
|
+
const GetAllPostsInteraction = Interaction.create({
|
|
4414
|
+
name: 'getAllPosts',
|
|
4415
|
+
action: GetAction,
|
|
4416
|
+
data: Post
|
|
4417
|
+
})
|
|
4418
|
+
|
|
4419
|
+
// 4. Create controller with eventSources and initialize
|
|
4132
4420
|
const controller = new Controller({
|
|
4133
|
-
system,
|
|
4134
|
-
entities: [User, Post],
|
|
4135
|
-
relations: [UserPostRelation, PostLikeRelation],
|
|
4136
|
-
|
|
4137
|
-
interactions: [CreatePostInteraction, LikePostInteraction] // Interactions
|
|
4421
|
+
system,
|
|
4422
|
+
entities: [User, Post],
|
|
4423
|
+
relations: [UserPostRelation, PostLikeRelation],
|
|
4424
|
+
eventSources: [CreatePostInteraction, LikePostInteraction, GetAllPostsInteraction]
|
|
4138
4425
|
})
|
|
4139
4426
|
|
|
4140
4427
|
await controller.setup(true)
|
|
4141
4428
|
|
|
4142
|
-
// 5. Use
|
|
4429
|
+
// 5. Use dispatch API
|
|
4143
4430
|
// Create post
|
|
4144
|
-
const result = await controller.
|
|
4431
|
+
const result = await controller.dispatch(CreatePostInteraction, {
|
|
4145
4432
|
user: { id: 'user1' },
|
|
4146
4433
|
payload: {
|
|
4147
4434
|
postData: {
|
|
@@ -4152,12 +4439,21 @@ const result = await controller.callInteraction('createPost', {
|
|
|
4152
4439
|
})
|
|
4153
4440
|
|
|
4154
4441
|
// Like post
|
|
4155
|
-
await controller.
|
|
4442
|
+
await controller.dispatch(LikePostInteraction, {
|
|
4156
4443
|
user: { id: 'user2' },
|
|
4157
4444
|
payload: {
|
|
4158
|
-
post: { id:
|
|
4445
|
+
post: { id: 'post1' }
|
|
4446
|
+
}
|
|
4447
|
+
})
|
|
4448
|
+
|
|
4449
|
+
// Get all posts
|
|
4450
|
+
const postsResult = await controller.dispatch(GetAllPostsInteraction, {
|
|
4451
|
+
user: { id: 'user1' },
|
|
4452
|
+
query: {
|
|
4453
|
+
attributeQuery: ['id', 'title', 'content', 'likeCount']
|
|
4159
4454
|
}
|
|
4160
4455
|
})
|
|
4456
|
+
// postsResult.data contains the posts
|
|
4161
4457
|
```
|
|
4162
4458
|
|
|
4163
4459
|
This API reference documentation covers all core APIs of the interaqt framework, providing complete parameter descriptions and practical usage examples. Developers can quickly get started and deeply use various framework features based on this documentation.
|