@cratis/chronicle 3.0.0 → 3.1.1
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/ChronicleClient.ts +4 -3
- package/EventStore.ts +20 -3
- package/IEventStore.ts +16 -0
- package/README.md +40 -28
- package/auditing/CausationType.ts +10 -0
- package/compliance/ComplianceContracts.ts +84 -0
- package/compliance/IPIIManager.ts +17 -0
- package/compliance/PIIManager.ts +31 -0
- package/compliance/index.ts +2 -0
- package/connection/ChronicleConnection.ts +7 -1
- package/connection/ChronicleServices.ts +2 -0
- package/connection/Guid.ts +18 -0
- package/connection/callResults.spec.ts +90 -0
- package/connection/callResults.ts +112 -0
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +4 -3
- package/dist/ChronicleClient.js.map +1 -1
- package/dist/EventStore.d.ts +8 -0
- package/dist/EventStore.d.ts.map +1 -1
- package/dist/EventStore.js +16 -3
- package/dist/EventStore.js.map +1 -1
- package/dist/IEventStore.d.ts +12 -0
- package/dist/IEventStore.d.ts.map +1 -1
- package/dist/auditing/CausationType.d.ts +8 -0
- package/dist/auditing/CausationType.d.ts.map +1 -1
- package/dist/auditing/CausationType.js +8 -0
- package/dist/auditing/CausationType.js.map +1 -1
- package/dist/compliance/ComplianceContracts.d.ts +40 -0
- package/dist/compliance/ComplianceContracts.d.ts.map +1 -1
- package/dist/compliance/ComplianceContracts.js +71 -0
- package/dist/compliance/ComplianceContracts.js.map +1 -1
- package/dist/compliance/IPIIManager.d.ts +15 -0
- package/dist/compliance/IPIIManager.d.ts.map +1 -0
- package/dist/compliance/IPIIManager.js +4 -0
- package/dist/compliance/IPIIManager.js.map +1 -0
- package/dist/compliance/PIIManager.d.ts +20 -0
- package/dist/compliance/PIIManager.d.ts.map +1 -0
- package/dist/compliance/PIIManager.js +30 -0
- package/dist/compliance/PIIManager.js.map +1 -0
- package/dist/compliance/index.d.ts +2 -0
- package/dist/compliance/index.d.ts.map +1 -1
- package/dist/compliance/index.js +1 -0
- package/dist/compliance/index.js.map +1 -1
- package/dist/connection/ChronicleConnection.d.ts +1 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +6 -1
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleServices.d.ts +2 -0
- package/dist/connection/ChronicleServices.d.ts.map +1 -1
- package/dist/connection/Guid.d.ts +5 -0
- package/dist/connection/Guid.d.ts.map +1 -1
- package/dist/connection/Guid.js +15 -0
- package/dist/connection/Guid.js.map +1 -1
- package/dist/connection/callResults.d.ts +69 -0
- package/dist/connection/callResults.d.ts.map +1 -0
- package/dist/connection/callResults.js +74 -0
- package/dist/connection/callResults.js.map +1 -0
- package/dist/eventSequences/AppendOperationsBroadcaster.d.ts +16 -0
- package/dist/eventSequences/AppendOperationsBroadcaster.d.ts.map +1 -0
- package/dist/eventSequences/AppendOperationsBroadcaster.js +77 -0
- package/dist/eventSequences/AppendOperationsBroadcaster.js.map +1 -0
- package/dist/eventSequences/AppendOptions.d.ts +7 -0
- package/dist/eventSequences/AppendOptions.d.ts.map +1 -1
- package/dist/eventSequences/AppendResult.d.ts +14 -0
- package/dist/eventSequences/AppendResult.d.ts.map +1 -1
- package/dist/eventSequences/AppendedEventWithResult.d.ts +13 -0
- package/dist/eventSequences/AppendedEventWithResult.d.ts.map +1 -0
- package/dist/eventSequences/AppendedEventWithResult.js +4 -0
- package/dist/eventSequences/AppendedEventWithResult.js.map +1 -0
- package/dist/eventSequences/CompleteStreamError.d.ts +17 -0
- package/dist/eventSequences/CompleteStreamError.d.ts.map +1 -0
- package/dist/eventSequences/CompleteStreamError.js +20 -0
- package/dist/eventSequences/CompleteStreamError.js.map +1 -0
- package/dist/eventSequences/CompleteStreamResult.d.ts +14 -0
- package/dist/eventSequences/CompleteStreamResult.d.ts.map +1 -0
- package/dist/eventSequences/CompleteStreamResult.js +4 -0
- package/dist/eventSequences/CompleteStreamResult.js.map +1 -0
- package/dist/eventSequences/ConcurrencyViolation.d.ts +13 -0
- package/dist/eventSequences/ConcurrencyViolation.d.ts.map +1 -0
- package/dist/eventSequences/ConcurrencyViolation.js +4 -0
- package/dist/eventSequences/ConcurrencyViolation.js.map +1 -0
- package/dist/eventSequences/EventForEventSourceId.d.ts +8 -0
- package/dist/eventSequences/EventForEventSourceId.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +35 -1
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +350 -19
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +85 -1
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/eventSequences/WaitForCompletionResult.d.ts +11 -0
- package/dist/eventSequences/WaitForCompletionResult.d.ts.map +1 -0
- package/dist/eventSequences/WaitForCompletionResult.js +4 -0
- package/dist/eventSequences/WaitForCompletionResult.js.map +1 -0
- package/dist/eventSequences/index.d.ts +5 -0
- package/dist/eventSequences/index.d.ts.map +1 -1
- package/dist/eventSequences/index.js +1 -0
- package/dist/eventSequences/index.js.map +1 -1
- package/dist/eventStoreSubscriptions/EventStoreSubscriptions.d.ts +3 -0
- package/dist/eventStoreSubscriptions/EventStoreSubscriptions.d.ts.map +1 -1
- package/dist/eventStoreSubscriptions/EventStoreSubscriptions.js +9 -0
- package/dist/eventStoreSubscriptions/EventStoreSubscriptions.js.map +1 -1
- package/dist/eventStoreSubscriptions/IEventStoreSubscriptions.d.ts +6 -0
- package/dist/eventStoreSubscriptions/IEventStoreSubscriptions.d.ts.map +1 -1
- package/dist/eventStoreSubscriptions/contracts.d.ts +18 -0
- package/dist/eventStoreSubscriptions/contracts.d.ts.map +1 -1
- package/dist/eventStoreSubscriptions/contracts.js +106 -0
- package/dist/eventStoreSubscriptions/contracts.js.map +1 -1
- package/dist/events/constraints/Constraints.js +4 -4
- package/dist/events/constraints/Constraints.js.map +1 -1
- package/dist/externalServices/ExternalServiceBuilder.d.ts +42 -0
- package/dist/externalServices/ExternalServiceBuilder.d.ts.map +1 -0
- package/dist/externalServices/ExternalServiceBuilder.js +110 -0
- package/dist/externalServices/ExternalServiceBuilder.js.map +1 -0
- package/dist/externalServices/ExternalServices.d.ts +19 -0
- package/dist/externalServices/ExternalServices.d.ts.map +1 -0
- package/dist/externalServices/ExternalServices.js +30 -0
- package/dist/externalServices/ExternalServices.js.map +1 -0
- package/dist/externalServices/ExternalServicesContracts.d.ts +137 -0
- package/dist/externalServices/ExternalServicesContracts.d.ts.map +1 -0
- package/dist/externalServices/ExternalServicesContracts.js +654 -0
- package/dist/externalServices/ExternalServicesContracts.js.map +1 -0
- package/dist/externalServices/IExternalServiceBuilder.d.ts +67 -0
- package/dist/externalServices/IExternalServiceBuilder.d.ts.map +1 -0
- package/dist/externalServices/IExternalServiceBuilder.js +4 -0
- package/dist/externalServices/IExternalServiceBuilder.js.map +1 -0
- package/dist/externalServices/IExternalServices.d.ts +13 -0
- package/dist/externalServices/IExternalServices.d.ts.map +1 -0
- package/dist/externalServices/IExternalServices.js +4 -0
- package/dist/externalServices/IExternalServices.js.map +1 -0
- package/dist/externalServices/index.d.ts +5 -0
- package/dist/externalServices/index.d.ts.map +1 -0
- package/dist/externalServices/index.js +5 -0
- package/dist/externalServices/index.js.map +1 -0
- package/dist/identities/IIdentityManager.d.ts +17 -0
- package/dist/identities/IIdentityManager.d.ts.map +1 -0
- package/dist/identities/IIdentityManager.js +4 -0
- package/dist/identities/IIdentityManager.js.map +1 -0
- package/dist/identities/IdentityManager.d.ts +20 -0
- package/dist/identities/IdentityManager.d.ts.map +1 -0
- package/dist/identities/IdentityManager.js +31 -0
- package/dist/identities/IdentityManager.js.map +1 -0
- package/dist/identities/index.d.ts +3 -0
- package/dist/identities/index.d.ts.map +1 -0
- package/dist/identities/index.js +4 -0
- package/dist/identities/index.js.map +1 -0
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/jobs/IJobs.d.ts +4 -4
- package/dist/jobs/IJobs.d.ts.map +1 -1
- package/dist/jobs/Jobs.d.ts +4 -4
- package/dist/jobs/Jobs.d.ts.map +1 -1
- package/dist/jobs/Jobs.js +12 -10
- package/dist/jobs/Jobs.js.map +1 -1
- package/dist/observation/FailedPartition.d.ts +15 -0
- package/dist/observation/FailedPartition.d.ts.map +1 -0
- package/dist/observation/FailedPartition.js +4 -0
- package/dist/observation/FailedPartition.js.map +1 -0
- package/dist/observation/FailedPartitionAttempt.d.ts +15 -0
- package/dist/observation/FailedPartitionAttempt.d.ts.map +1 -0
- package/dist/observation/FailedPartitionAttempt.js +4 -0
- package/dist/observation/FailedPartitionAttempt.js.map +1 -0
- package/dist/observation/FailedPartitions.d.ts +24 -0
- package/dist/observation/FailedPartitions.d.ts.map +1 -0
- package/dist/observation/FailedPartitions.js +39 -0
- package/dist/observation/FailedPartitions.js.map +1 -0
- package/dist/observation/ICanBeNotifiedWhenPartitionReplayed.d.ts +19 -0
- package/dist/observation/ICanBeNotifiedWhenPartitionReplayed.d.ts.map +1 -0
- package/dist/observation/ICanBeNotifiedWhenPartitionReplayed.js +4 -0
- package/dist/observation/ICanBeNotifiedWhenPartitionReplayed.js.map +1 -0
- package/dist/observation/ICanBeNotifiedWhenReplay.d.ts +17 -0
- package/dist/observation/ICanBeNotifiedWhenReplay.d.ts.map +1 -0
- package/dist/observation/ICanBeNotifiedWhenReplay.js +4 -0
- package/dist/observation/ICanBeNotifiedWhenReplay.js.map +1 -0
- package/dist/observation/IFailedPartitions.d.ts +18 -0
- package/dist/observation/IFailedPartitions.d.ts.map +1 -0
- package/dist/observation/IFailedPartitions.js +4 -0
- package/dist/observation/IFailedPartitions.js.map +1 -0
- package/dist/observation/index.d.ts +6 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +1 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/observation/notifyReplayLifecycle.d.ts +13 -0
- package/dist/observation/notifyReplayLifecycle.d.ts.map +1 -0
- package/dist/observation/notifyReplayLifecycle.js +54 -0
- package/dist/observation/notifyReplayLifecycle.js.map +1 -0
- package/dist/observation/toClientFailedPartition.d.ts +9 -0
- package/dist/observation/toClientFailedPartition.d.ts.map +1 -0
- package/dist/observation/toClientFailedPartition.js +23 -0
- package/dist/observation/toClientFailedPartition.js.map +1 -0
- package/dist/projections/Projections.d.ts +0 -4
- package/dist/projections/Projections.d.ts.map +1 -1
- package/dist/projections/Projections.js +25 -103
- package/dist/projections/Projections.js.map +1 -1
- package/dist/projections/declarative/AddBuilder.d.ts +17 -0
- package/dist/projections/declarative/AddBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/AddBuilder.js +28 -0
- package/dist/projections/declarative/AddBuilder.js.map +1 -0
- package/dist/projections/declarative/AddChildBuilder.d.ts +56 -0
- package/dist/projections/declarative/AddChildBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/AddChildBuilder.js +72 -0
- package/dist/projections/declarative/AddChildBuilder.js.map +1 -0
- package/dist/projections/declarative/ChildrenBuilder.d.ts +28 -0
- package/dist/projections/declarative/ChildrenBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/ChildrenBuilder.js +72 -0
- package/dist/projections/declarative/ChildrenBuilder.js.map +1 -0
- package/dist/projections/declarative/CompositeKeyBuilder.d.ts +16 -0
- package/dist/projections/declarative/CompositeKeyBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/CompositeKeyBuilder.js +29 -0
- package/dist/projections/declarative/CompositeKeyBuilder.js.map +1 -0
- package/dist/projections/declarative/FromBuilder.d.ts +8 -6
- package/dist/projections/declarative/FromBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/FromBuilder.js +44 -14
- package/dist/projections/declarative/FromBuilder.js.map +1 -1
- package/dist/projections/declarative/ICompositeKeyBuilder.d.ts +5 -0
- package/dist/projections/declarative/ICompositeKeyBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/JoinBuilder.d.ts +7 -5
- package/dist/projections/declarative/JoinBuilder.d.ts.map +1 -1
- package/dist/projections/declarative/JoinBuilder.js +50 -16
- package/dist/projections/declarative/JoinBuilder.js.map +1 -1
- package/dist/projections/declarative/NestedBuilder.d.ts +24 -0
- package/dist/projections/declarative/NestedBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/NestedBuilder.js +60 -0
- package/dist/projections/declarative/NestedBuilder.js.map +1 -0
- package/dist/projections/declarative/ProjectionBuilderCore.d.ts +127 -0
- package/dist/projections/declarative/ProjectionBuilderCore.d.ts.map +1 -0
- package/dist/projections/declarative/ProjectionBuilderCore.js +180 -0
- package/dist/projections/declarative/ProjectionBuilderCore.js.map +1 -0
- package/dist/projections/declarative/ProjectionBuilderFor.d.ts +6 -32
- package/dist/projections/declarative/ProjectionBuilderFor.d.ts.map +1 -1
- package/dist/projections/declarative/ProjectionBuilderFor.js +24 -118
- package/dist/projections/declarative/ProjectionBuilderFor.js.map +1 -1
- package/dist/projections/declarative/SubtractBuilder.d.ts +17 -0
- package/dist/projections/declarative/SubtractBuilder.d.ts.map +1 -0
- package/dist/projections/declarative/SubtractBuilder.js +28 -0
- package/dist/projections/declarative/SubtractBuilder.js.map +1 -0
- package/dist/projections/declarative/index.d.ts +9 -0
- package/dist/projections/declarative/index.d.ts.map +1 -1
- package/dist/projections/declarative/index.js +8 -0
- package/dist/projections/declarative/index.js.map +1 -1
- package/dist/projections/modelBound/childrenAndNestedBuilder.d.ts +90 -0
- package/dist/projections/modelBound/childrenAndNestedBuilder.d.ts.map +1 -0
- package/dist/projections/modelBound/childrenAndNestedBuilder.js +290 -0
- package/dist/projections/modelBound/childrenAndNestedBuilder.js.map +1 -0
- package/dist/reactors/ReactorSideEffects.d.ts +29 -0
- package/dist/reactors/ReactorSideEffects.d.ts.map +1 -0
- package/dist/reactors/ReactorSideEffects.js +70 -0
- package/dist/reactors/ReactorSideEffects.js.map +1 -0
- package/dist/reactors/Reactors.d.ts +4 -1
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +23 -4
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +15 -3
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/reducer.d.ts +9 -1
- package/dist/reducers/reducer.d.ts.map +1 -1
- package/dist/reducers/reducer.js +4 -2
- package/dist/reducers/reducer.js.map +1 -1
- package/dist/transactions/IUnitOfWork.d.ts +18 -0
- package/dist/transactions/IUnitOfWork.d.ts.map +1 -1
- package/dist/transactions/UnitOfWork.d.ts +9 -0
- package/dist/transactions/UnitOfWork.d.ts.map +1 -1
- package/dist/transactions/UnitOfWork.js +14 -0
- package/dist/transactions/UnitOfWork.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/AppendOperationsBroadcaster.ts +83 -0
- package/eventSequences/AppendOptions.ts +8 -0
- package/eventSequences/AppendResult.ts +16 -0
- package/eventSequences/AppendedEventWithResult.ts +17 -0
- package/eventSequences/CompleteStreamError.ts +20 -0
- package/eventSequences/CompleteStreamResult.ts +13 -0
- package/eventSequences/ConcurrencyViolation.ts +18 -0
- package/eventSequences/EventForEventSourceId.ts +12 -0
- package/eventSequences/EventSequence.spec.ts +588 -0
- package/eventSequences/EventSequence.ts +406 -20
- package/eventSequences/IEventSequence.ts +109 -1
- package/eventSequences/WaitForCompletionResult.ts +15 -0
- package/eventSequences/index.ts +5 -0
- package/eventStoreSubscriptions/EventStoreSubscriptions.spec.ts +52 -0
- package/eventStoreSubscriptions/EventStoreSubscriptions.ts +19 -1
- package/eventStoreSubscriptions/IEventStoreSubscriptions.ts +7 -0
- package/eventStoreSubscriptions/contracts.ts +126 -0
- package/events/constraints/Constraints.ts +4 -4
- package/externalServices/ExternalServiceBuilder.ts +128 -0
- package/externalServices/ExternalServices.ts +34 -0
- package/externalServices/ExternalServicesContracts.ts +709 -0
- package/externalServices/IExternalServiceBuilder.ts +76 -0
- package/externalServices/IExternalServices.ts +16 -0
- package/externalServices/index.ts +7 -0
- package/identities/IIdentityManager.ts +19 -0
- package/identities/IdentityManager.ts +32 -0
- package/identities/index.ts +5 -0
- package/index.ts +6 -0
- package/jobs/IJobs.ts +4 -4
- package/jobs/Jobs.ts +16 -14
- package/observation/FailedPartition.ts +21 -0
- package/observation/FailedPartitionAttempt.ts +21 -0
- package/observation/FailedPartitions.spec.ts +75 -0
- package/observation/FailedPartitions.ts +44 -0
- package/observation/ICanBeNotifiedWhenPartitionReplayed.ts +22 -0
- package/observation/ICanBeNotifiedWhenReplay.ts +20 -0
- package/observation/IFailedPartitions.ts +22 -0
- package/observation/index.ts +6 -0
- package/observation/notifyReplayLifecycle.spec.ts +98 -0
- package/observation/notifyReplayLifecycle.ts +61 -0
- package/observation/toClientFailedPartition.ts +26 -0
- package/package.json +28 -3
- package/projections/Projections.childrenAndNested.spec.ts +188 -0
- package/projections/Projections.spec.ts +131 -0
- package/projections/Projections.ts +37 -119
- package/projections/declarative/AddBuilder.ts +28 -0
- package/projections/declarative/AddChildBuilder.ts +97 -0
- package/projections/declarative/ChildrenBuilder.spec.ts +157 -0
- package/projections/declarative/ChildrenBuilder.ts +90 -0
- package/projections/declarative/CompositeKeyBuilder.spec.ts +117 -0
- package/projections/declarative/CompositeKeyBuilder.ts +38 -0
- package/projections/declarative/FromBuilder.ts +60 -15
- package/projections/declarative/ICompositeKeyBuilder.ts +6 -0
- package/projections/declarative/JoinBuilder.ts +66 -17
- package/projections/declarative/NestedBuilder.ts +76 -0
- package/projections/declarative/ProjectionBuilderCore.ts +257 -0
- package/projections/declarative/ProjectionBuilderFor.spec.ts +115 -0
- package/projections/declarative/ProjectionBuilderFor.ts +29 -168
- package/projections/declarative/SubtractBuilder.ts +28 -0
- package/projections/declarative/index.ts +9 -0
- package/projections/modelBound/childrenAndNestedBuilder.ts +354 -0
- package/reactors/ReactorSideEffects.spec.ts +182 -0
- package/reactors/ReactorSideEffects.ts +97 -0
- package/reactors/Reactors.ts +29 -4
- package/reducers/Reducers.spec.ts +87 -0
- package/reducers/Reducers.ts +15 -3
- package/reducers/reducer.spec.ts +40 -0
- package/reducers/reducer.ts +11 -2
- package/transactions/IUnitOfWork.ts +21 -0
- package/transactions/UnitOfWork.spec.ts +82 -0
- package/transactions/UnitOfWork.ts +20 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import 'reflect-metadata';
|
|
5
|
+
import { AutoMap } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { Constructor, Fields } from '@cratis/fundamentals';
|
|
7
|
+
import { TypeIntrospector } from '../../types';
|
|
8
|
+
import { getEventTypeFor } from '../../events/eventTypeDecorator';
|
|
9
|
+
import { getAddFromMetadata } from './addFrom';
|
|
10
|
+
import { ChildrenFromMetadata, getChildrenFromMetadata } from './childrenFrom';
|
|
11
|
+
import { getClearWithClassMetadata, getClearWithPropertyMetadata } from './clearWith';
|
|
12
|
+
import { getCountMetadata } from './count';
|
|
13
|
+
import { getDecrementMetadata } from './decrement';
|
|
14
|
+
import { getFromEventMetadata } from './fromEvent';
|
|
15
|
+
import { getIncrementMetadata } from './increment';
|
|
16
|
+
import { isNested } from './nested';
|
|
17
|
+
import { getRemovedWithClassMetadata, getRemovedWithPropertyMetadata } from './removedWith';
|
|
18
|
+
import { getRemovedWithJoinClassMetadata, getRemovedWithJoinPropertyMetadata } from './removedWithJoin';
|
|
19
|
+
import { getSetFromMetadata } from './setFrom';
|
|
20
|
+
import { getSetFromContextMetadata } from './setFromContext';
|
|
21
|
+
import { getSetValueMetadata } from './setValue';
|
|
22
|
+
import { getSubtractFromMetadata } from './subtractFrom';
|
|
23
|
+
|
|
24
|
+
/** The contract-level event type identifier shape used across projection definitions. */
|
|
25
|
+
export type ContractEventType = { Id: string; Generation: number; Tombstone: boolean };
|
|
26
|
+
|
|
27
|
+
/** Accumulated property mapping for a from clause. */
|
|
28
|
+
export interface FromRecord {
|
|
29
|
+
Key: ContractEventType;
|
|
30
|
+
Value: { Properties: Record<string, string>; Key: string; ParentKey: string };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Plain object shape matching the wire-level `ChildrenDefinition`. */
|
|
34
|
+
export interface ChildrenDefinitionLike {
|
|
35
|
+
IdentifiedBy: string;
|
|
36
|
+
From: FromRecord[];
|
|
37
|
+
Join: unknown[];
|
|
38
|
+
Children: Record<string, ChildrenDefinitionLike>;
|
|
39
|
+
All: { Properties: Record<string, string>; IncludeChildren: boolean; AutoMap: AutoMap };
|
|
40
|
+
RemovedWith: Array<{ Key: ContractEventType; Value: { Key: string; ParentKey: string } }>;
|
|
41
|
+
RemovedWithJoin: Array<{ Key: ContractEventType; Value: { Key: string } }>;
|
|
42
|
+
AutoMap: AutoMap;
|
|
43
|
+
Nested: Record<string, ChildrenDefinitionLike>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the contract event type for a decorated event constructor.
|
|
48
|
+
* @param eventTypeConstructor - The event class constructor.
|
|
49
|
+
* @returns The contract-level event type identifier.
|
|
50
|
+
*/
|
|
51
|
+
export function toContractEventType(eventTypeConstructor: Function): ContractEventType {
|
|
52
|
+
const eventType = getEventTypeFor(eventTypeConstructor);
|
|
53
|
+
if (eventType.id.value === '') {
|
|
54
|
+
throw new Error(`Event type '${eventTypeConstructor.name}' is not decorated with @eventType().`);
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
Id: eventType.id.value,
|
|
58
|
+
Generation: eventType.generation.value,
|
|
59
|
+
Tombstone: false
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Builds a stable map key for a contract event type.
|
|
65
|
+
* @param eventType - The contract event type.
|
|
66
|
+
* @returns A string key unique to the event type/generation/tombstone combination.
|
|
67
|
+
*/
|
|
68
|
+
export function getEventTypeMapKey(eventType: ContractEventType): string {
|
|
69
|
+
return `${eventType.Id}:${eventType.Generation}:${eventType.Tombstone ? '1' : '0'}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Gets or creates the `From` entry for an event type within the given map.
|
|
74
|
+
* @param fromByEventType - The map of event type key to accumulated from entry.
|
|
75
|
+
* @param eventTypeConstructor - The event constructor to resolve the entry for.
|
|
76
|
+
* @returns The existing or newly created from entry.
|
|
77
|
+
*/
|
|
78
|
+
export function ensureFromEntry(fromByEventType: Map<string, FromRecord>, eventTypeConstructor: Function): FromRecord {
|
|
79
|
+
const eventType = toContractEventType(eventTypeConstructor);
|
|
80
|
+
const key = getEventTypeMapKey(eventType);
|
|
81
|
+
const existing = fromByEventType.get(key);
|
|
82
|
+
if (existing) {
|
|
83
|
+
return existing;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const created: FromRecord = { Key: eventType, Value: { Properties: {}, Key: '$eventSourceId', ParentKey: '' } };
|
|
87
|
+
fromByEventType.set(key, created);
|
|
88
|
+
return created;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Applies the property-mapping decorators (`@setFrom`, `@setFromContext`, `@setValue`,
|
|
93
|
+
* `@addFrom`, `@subtractFrom`, `@increment`, `@decrement`, `@count`) declared on a single
|
|
94
|
+
* property into the given from-by-event-type map. Shared by the root model-bound processing
|
|
95
|
+
* loop and by child/nested type processing below, so both honor the same decorators.
|
|
96
|
+
* @param prototype - The class prototype the property is declared on.
|
|
97
|
+
* @param property - The property name.
|
|
98
|
+
* @param fromByEventType - The map of event type key to accumulated from entry to populate.
|
|
99
|
+
*/
|
|
100
|
+
export function applyPropertyMappings(prototype: object, property: string, fromByEventType: Map<string, FromRecord>): void {
|
|
101
|
+
for (const mapping of getSetFromMetadata(prototype, property)) {
|
|
102
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
103
|
+
entry.Value.Properties[property] = mapping.eventPropertyName ?? property;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
for (const mapping of getSetFromContextMetadata(prototype, property)) {
|
|
107
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
108
|
+
entry.Value.Properties[property] = mapping.contextPropertyName ?? property;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
for (const mapping of getSetValueMetadata(prototype, property)) {
|
|
112
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
113
|
+
entry.Value.Properties[property] = JSON.stringify(mapping.value);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const mapping of getAddFromMetadata(prototype, property)) {
|
|
117
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
118
|
+
entry.Value.Properties[property] = `$add(${mapping.eventPropertyName ?? property})`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
for (const mapping of getSubtractFromMetadata(prototype, property)) {
|
|
122
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
123
|
+
entry.Value.Properties[property] = `$subtract(${mapping.eventPropertyName ?? property})`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
for (const mapping of getIncrementMetadata(prototype, property)) {
|
|
127
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
128
|
+
entry.Value.Properties[property] = '$increment';
|
|
129
|
+
if (mapping.constantKey) {
|
|
130
|
+
entry.Value.Key = `$value(${mapping.constantKey})`;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
for (const mapping of getDecrementMetadata(prototype, property)) {
|
|
135
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
136
|
+
entry.Value.Properties[property] = '$decrement';
|
|
137
|
+
if (mapping.constantKey) {
|
|
138
|
+
entry.Value.Key = `$value(${mapping.constantKey})`;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
for (const mapping of getCountMetadata(prototype, property)) {
|
|
143
|
+
const entry = ensureFromEntry(fromByEventType, mapping.eventType);
|
|
144
|
+
entry.Value.Properties[property] = '$count';
|
|
145
|
+
if (mapping.constantKey) {
|
|
146
|
+
entry.Value.Key = `$value(${mapping.constantKey})`;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function createEmptyChildrenDefinition(): ChildrenDefinitionLike {
|
|
152
|
+
return {
|
|
153
|
+
IdentifiedBy: '$eventSourceId',
|
|
154
|
+
From: [],
|
|
155
|
+
Join: [],
|
|
156
|
+
Children: {},
|
|
157
|
+
All: { Properties: {}, IncludeChildren: false, AutoMap: AutoMap.Inherit },
|
|
158
|
+
RemovedWith: [],
|
|
159
|
+
RemovedWithJoin: [],
|
|
160
|
+
AutoMap: AutoMap.Enabled,
|
|
161
|
+
Nested: {}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Resolves the element type of a `@childrenFrom` collection property. TypeScript erases
|
|
167
|
+
* generic type arguments at runtime, so the element type can only be recovered when the
|
|
168
|
+
* property was declared with `@field(Array, { genericArguments: [ItemType] })` - without it,
|
|
169
|
+
* the children definition still registers correctly (structural Key/ParentKey wiring plus
|
|
170
|
+
* AutoMap), it just cannot also translate the child type's own decorators.
|
|
171
|
+
* @param type - The declaring class constructor.
|
|
172
|
+
* @param property - The property name.
|
|
173
|
+
* @returns The child element type constructor, or undefined when it cannot be resolved.
|
|
174
|
+
*/
|
|
175
|
+
function resolveChildElementType(type: Function, property: string): Function | undefined {
|
|
176
|
+
const field = Fields.getFieldsForType(type as Constructor).find(candidate => candidate.name === property);
|
|
177
|
+
return field?.genericArguments?.[0];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Resolves the type of a `@nested` single-object property, preferring an explicit
|
|
182
|
+
* `@field(NestedType)` declaration and falling back to `design:type` decorator metadata
|
|
183
|
+
* (only available when compiled with `tsc`, not esbuild/tsx).
|
|
184
|
+
* @param type - The declaring class constructor.
|
|
185
|
+
* @param property - The property name.
|
|
186
|
+
* @returns The nested type constructor, or undefined when it cannot be resolved.
|
|
187
|
+
*/
|
|
188
|
+
function resolveNestedType(type: Function, property: string): Function | undefined {
|
|
189
|
+
const field = Fields.getFieldsForType(type as Constructor).find(candidate => candidate.name === property);
|
|
190
|
+
if (field?.type && field.type !== Object && field.type !== Array) {
|
|
191
|
+
return field.type;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const designType = Reflect.getMetadata('design:type', type.prototype, property) as Function | undefined;
|
|
195
|
+
if (designType && designType !== Object) {
|
|
196
|
+
return designType;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Discovers the child model property used to identify instances, by convention: a property
|
|
204
|
+
* named `id` (case-insensitive), matching the C# client's fallback once no `[Key]` attribute
|
|
205
|
+
* is present (TypeScript has no `[Key]` decorator equivalent).
|
|
206
|
+
* @param childType - The child/nested model type, when resolvable.
|
|
207
|
+
* @returns The discovered property name, or undefined when no convention match is found.
|
|
208
|
+
*/
|
|
209
|
+
function discoverIdentifiedBy(childType: Function | undefined): string | undefined {
|
|
210
|
+
if (!childType) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for (const name of TypeIntrospector.getMembers(childType).keys()) {
|
|
215
|
+
if (name.toLowerCase() === 'id') {
|
|
216
|
+
return name;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Populates a children/nested definition from a resolved child/nested type's own class-level
|
|
225
|
+
* and property-level decorators (`@fromEvent`, `@clearWith`, `@removedWith`,
|
|
226
|
+
* `@removedWithJoin`, and the property-mapping decorators), recursing into any further
|
|
227
|
+
* `@childrenFrom`/`@nested` properties the child/nested type itself declares.
|
|
228
|
+
* @param definition - The children definition being populated.
|
|
229
|
+
* @param childType - The resolved child/nested type, when available.
|
|
230
|
+
*/
|
|
231
|
+
function populateFromType(definition: ChildrenDefinitionLike, childType: Function | undefined): void {
|
|
232
|
+
if (!childType) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const fromByEventType = new Map<string, FromRecord>();
|
|
237
|
+
for (const entry of definition.From) {
|
|
238
|
+
fromByEventType.set(getEventTypeMapKey(entry.Key), entry);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
for (const fromEvent of getFromEventMetadata(childType)) {
|
|
242
|
+
ensureFromEntry(fromByEventType, fromEvent.eventType);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const removedWithByEventType = new Map<string, { Key: ContractEventType; Value: { Key: string; ParentKey: string } }>();
|
|
246
|
+
for (const entry of definition.RemovedWith) {
|
|
247
|
+
removedWithByEventType.set(getEventTypeMapKey(entry.Key), entry);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
for (const clearWith of getClearWithClassMetadata(childType)) {
|
|
251
|
+
const eventType = toContractEventType(clearWith.eventType);
|
|
252
|
+
removedWithByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: '$eventSourceId', ParentKey: '' } });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
for (const removed of getRemovedWithClassMetadata(childType)) {
|
|
256
|
+
const eventType = toContractEventType(removed.eventType);
|
|
257
|
+
removedWithByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: removed.key ?? '$eventSourceId', ParentKey: removed.parentKey ?? '' } });
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const removedWithJoinByEventType = new Map<string, { Key: ContractEventType; Value: { Key: string } }>();
|
|
261
|
+
for (const entry of definition.RemovedWithJoin) {
|
|
262
|
+
removedWithJoinByEventType.set(getEventTypeMapKey(entry.Key), entry);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (const removed of getRemovedWithJoinClassMetadata(childType)) {
|
|
266
|
+
const eventType = toContractEventType(removed.eventType);
|
|
267
|
+
removedWithJoinByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: removed.key ?? '$eventSourceId' } });
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const prototype = childType.prototype;
|
|
271
|
+
for (const property of TypeIntrospector.getTrackedProperties(childType)) {
|
|
272
|
+
applyPropertyMappings(prototype, property, fromByEventType);
|
|
273
|
+
|
|
274
|
+
for (const clearWith of getClearWithPropertyMetadata(prototype, property)) {
|
|
275
|
+
const eventType = toContractEventType(clearWith.eventType);
|
|
276
|
+
removedWithByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: '$eventSourceId', ParentKey: '' } });
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
for (const removed of getRemovedWithPropertyMetadata(prototype, property)) {
|
|
280
|
+
const eventType = toContractEventType(removed.eventType);
|
|
281
|
+
removedWithByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: removed.key ?? '$eventSourceId', ParentKey: removed.parentKey ?? '' } });
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const removed of getRemovedWithJoinPropertyMetadata(prototype, property)) {
|
|
285
|
+
const eventType = toContractEventType(removed.eventType);
|
|
286
|
+
removedWithJoinByEventType.set(getEventTypeMapKey(eventType), { Key: eventType, Value: { Key: removed.key ?? '$eventSourceId' } });
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const childrenFromList = getChildrenFromMetadata(prototype, property);
|
|
290
|
+
if (childrenFromList.length > 0) {
|
|
291
|
+
definition.Children[property] = buildChildrenEntry(childType, property, childrenFromList);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (isNested(prototype, property)) {
|
|
295
|
+
definition.Nested[property] = buildNestedEntry(childType, property);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
definition.From = Array.from(fromByEventType.values());
|
|
300
|
+
definition.RemovedWith = Array.from(removedWithByEventType.values());
|
|
301
|
+
definition.RemovedWithJoin = Array.from(removedWithJoinByEventType.values());
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Builds the `ChildrenDefinition` for a `@childrenFrom` collection property.
|
|
306
|
+
* @param type - The class declaring the property.
|
|
307
|
+
* @param property - The property name.
|
|
308
|
+
* @param metadataList - The `@childrenFrom` metadata entries declared on the property (one per creating event type).
|
|
309
|
+
* @returns The wire-shaped children definition.
|
|
310
|
+
*/
|
|
311
|
+
export function buildChildrenEntry(type: Function, property: string, metadataList: ChildrenFromMetadata[]): ChildrenDefinitionLike {
|
|
312
|
+
const childType = resolveChildElementType(type, property);
|
|
313
|
+
const definition = createEmptyChildrenDefinition();
|
|
314
|
+
|
|
315
|
+
const explicitIdentifiedBy = metadataList.find(metadata => metadata.identifiedBy)?.identifiedBy;
|
|
316
|
+
definition.IdentifiedBy = explicitIdentifiedBy ?? discoverIdentifiedBy(childType) ?? '$eventSourceId';
|
|
317
|
+
|
|
318
|
+
for (const metadata of metadataList) {
|
|
319
|
+
const eventType = toContractEventType(metadata.eventType);
|
|
320
|
+
definition.From.push({
|
|
321
|
+
Key: eventType,
|
|
322
|
+
Value: {
|
|
323
|
+
Key: metadata.key ?? '$eventSourceId',
|
|
324
|
+
ParentKey: metadata.parentKey ?? '$eventSourceId',
|
|
325
|
+
Properties: {}
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
populateFromType(definition, childType);
|
|
331
|
+
return definition;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Builds the `ChildrenDefinition` for a `@nested` single-object property.
|
|
336
|
+
* @param type - The class declaring the property.
|
|
337
|
+
* @param property - The property name.
|
|
338
|
+
* @returns The wire-shaped nested definition.
|
|
339
|
+
*/
|
|
340
|
+
export function buildNestedEntry(type: Function, property: string): ChildrenDefinitionLike {
|
|
341
|
+
const nestedType = resolveNestedType(type, property);
|
|
342
|
+
const definition = createEmptyChildrenDefinition();
|
|
343
|
+
definition.IdentifiedBy = '';
|
|
344
|
+
|
|
345
|
+
// A @clearWith on the property carrying @nested clears this nested object, the same as a
|
|
346
|
+
// class-level @clearWith on the nested type itself (which populateFromType also honors).
|
|
347
|
+
for (const clearWith of getClearWithPropertyMetadata(type.prototype, property)) {
|
|
348
|
+
const eventType = toContractEventType(clearWith.eventType);
|
|
349
|
+
definition.RemovedWith.push({ Key: eventType, Value: { Key: '$eventSourceId', ParentKey: '' } });
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
populateFromType(definition, nestedType);
|
|
353
|
+
return definition;
|
|
354
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import type { IEventLog } from '../eventSequences/IEventLog';
|
|
6
|
+
import { EventSequenceNumber } from '../eventSequences/EventSequenceNumber';
|
|
7
|
+
import { eventType } from '../events/eventTypeDecorator';
|
|
8
|
+
import { appendReactorSideEffects } from './ReactorSideEffects';
|
|
9
|
+
|
|
10
|
+
class SomethingHappened {
|
|
11
|
+
constructor(readonly value: string = '') {}
|
|
12
|
+
}
|
|
13
|
+
eventType('c3f6a2f0-6f2f-4a3c-9d3f-6f2f4a3c9d3h')(SomethingHappened);
|
|
14
|
+
|
|
15
|
+
class SomethingElseHappened {
|
|
16
|
+
constructor(readonly value: string = '') {}
|
|
17
|
+
}
|
|
18
|
+
eventType('d3f6a2f0-6f2f-4a3c-9d3f-6f2f4a3c9d3i')(SomethingElseHappened);
|
|
19
|
+
|
|
20
|
+
function createEventLog(appendManyResult?: unknown) {
|
|
21
|
+
const appendMany = vi.fn().mockResolvedValue(appendManyResult ?? [
|
|
22
|
+
{ sequenceNumber: new EventSequenceNumber(1n), constraintViolations: [], errors: [], isSuccess: true }
|
|
23
|
+
]);
|
|
24
|
+
const eventLog = { appendMany } as unknown as IEventLog;
|
|
25
|
+
return { eventLog, appendMany };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const successResultFor = (count: number) =>
|
|
29
|
+
Array.from({ length: count }, (_, index) => ({
|
|
30
|
+
sequenceNumber: new EventSequenceNumber(BigInt(index + 1)),
|
|
31
|
+
constraintViolations: [],
|
|
32
|
+
errors: [],
|
|
33
|
+
isSuccess: true
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
describe('appendReactorSideEffects', () => {
|
|
37
|
+
describe('when the handler returns undefined', () => {
|
|
38
|
+
const { eventLog, appendMany } = createEventLog();
|
|
39
|
+
|
|
40
|
+
it('should not append anything', async () => {
|
|
41
|
+
const result = await appendReactorSideEffects(eventLog, undefined, 'triggering-source', 'Default', 'triggering-source');
|
|
42
|
+
|
|
43
|
+
expect(appendMany).not.toHaveBeenCalled();
|
|
44
|
+
expect(result.isSuccess).toBe(true);
|
|
45
|
+
expect(result.errors).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('when the handler returns a single bare event', () => {
|
|
50
|
+
const { eventLog, appendMany } = createEventLog(successResultFor(1));
|
|
51
|
+
|
|
52
|
+
it('should append it targeting the triggering event\'s event source, stream, and subject', async () => {
|
|
53
|
+
const result = await appendReactorSideEffects(
|
|
54
|
+
eventLog,
|
|
55
|
+
new SomethingHappened('a'),
|
|
56
|
+
'triggering-source',
|
|
57
|
+
'custom-stream-type',
|
|
58
|
+
'custom-stream-id');
|
|
59
|
+
|
|
60
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
61
|
+
const [events] = appendMany.mock.calls[0];
|
|
62
|
+
expect(events).toEqual([{
|
|
63
|
+
eventSourceId: 'triggering-source',
|
|
64
|
+
event: expect.any(SomethingHappened),
|
|
65
|
+
eventStreamType: 'custom-stream-type',
|
|
66
|
+
eventStreamId: 'custom-stream-id',
|
|
67
|
+
subject: 'triggering-source'
|
|
68
|
+
}]);
|
|
69
|
+
expect(result.isSuccess).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('when the handler returns an array of bare events', () => {
|
|
74
|
+
const { eventLog, appendMany } = createEventLog(successResultFor(2));
|
|
75
|
+
|
|
76
|
+
it('should append all of them in a single atomic AppendMany call', async () => {
|
|
77
|
+
await appendReactorSideEffects(
|
|
78
|
+
eventLog,
|
|
79
|
+
[new SomethingHappened('a'), new SomethingElseHappened('b')],
|
|
80
|
+
'triggering-source',
|
|
81
|
+
'Default',
|
|
82
|
+
'triggering-source');
|
|
83
|
+
|
|
84
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
85
|
+
const [events] = appendMany.mock.calls[0];
|
|
86
|
+
expect(events).toHaveLength(2);
|
|
87
|
+
expect(events[0].event).toBeInstanceOf(SomethingHappened);
|
|
88
|
+
expect(events[1].event).toBeInstanceOf(SomethingElseHappened);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('when the handler returns a single EventForEventSourceId', () => {
|
|
93
|
+
const { eventLog, appendMany } = createEventLog(successResultFor(1));
|
|
94
|
+
|
|
95
|
+
it('should append it using its own target rather than the triggering event\'s', async () => {
|
|
96
|
+
const wrapper = {
|
|
97
|
+
eventSourceId: 'other-source',
|
|
98
|
+
event: new SomethingHappened('a'),
|
|
99
|
+
eventStreamType: 'other-stream-type',
|
|
100
|
+
eventStreamId: 'other-stream-id',
|
|
101
|
+
subject: 'other-subject'
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await appendReactorSideEffects(eventLog, wrapper, 'triggering-source', 'Default', 'triggering-source');
|
|
105
|
+
|
|
106
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
107
|
+
const [events] = appendMany.mock.calls[0];
|
|
108
|
+
expect(events).toEqual([wrapper]);
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('when the handler returns an array of EventForEventSourceId', () => {
|
|
113
|
+
const { eventLog, appendMany } = createEventLog(successResultFor(2));
|
|
114
|
+
|
|
115
|
+
it('should append all of them, each keeping its own target, in one atomic call', async () => {
|
|
116
|
+
const wrappers = [
|
|
117
|
+
{ eventSourceId: 'source-1', event: new SomethingHappened('a') },
|
|
118
|
+
{ eventSourceId: 'source-2', event: new SomethingElseHappened('b') }
|
|
119
|
+
];
|
|
120
|
+
|
|
121
|
+
await appendReactorSideEffects(eventLog, wrappers, 'triggering-source', 'Default', 'triggering-source');
|
|
122
|
+
|
|
123
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
124
|
+
const [events] = appendMany.mock.calls[0];
|
|
125
|
+
expect(events).toEqual(wrappers);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('when the handler returns a mix of bare events and EventForEventSourceId', () => {
|
|
130
|
+
const { eventLog, appendMany } = createEventLog(successResultFor(2));
|
|
131
|
+
|
|
132
|
+
it('should append the bare event using the triggering context and the wrapper using its own, in one atomic call', async () => {
|
|
133
|
+
const wrapper = { eventSourceId: 'other-source', event: new SomethingElseHappened('b') };
|
|
134
|
+
const bareEvent = new SomethingHappened('a');
|
|
135
|
+
|
|
136
|
+
await appendReactorSideEffects(eventLog, [bareEvent, wrapper], 'triggering-source', 'Default', 'triggering-source');
|
|
137
|
+
|
|
138
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
139
|
+
const [events] = appendMany.mock.calls[0];
|
|
140
|
+
expect(events).toEqual([
|
|
141
|
+
{
|
|
142
|
+
eventSourceId: 'triggering-source',
|
|
143
|
+
event: bareEvent,
|
|
144
|
+
eventStreamType: 'Default',
|
|
145
|
+
eventStreamId: 'triggering-source',
|
|
146
|
+
subject: 'triggering-source'
|
|
147
|
+
},
|
|
148
|
+
wrapper
|
|
149
|
+
]);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('when a side-effect event fails to append', () => {
|
|
154
|
+
const { eventLog, appendMany } = createEventLog([
|
|
155
|
+
{
|
|
156
|
+
sequenceNumber: EventSequenceNumber.unset,
|
|
157
|
+
constraintViolations: [{ constraintId: 'unique', message: 'Value must be unique', details: {} }],
|
|
158
|
+
errors: [],
|
|
159
|
+
isSuccess: false
|
|
160
|
+
}
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
it('should report failure with the constraint violation message', async () => {
|
|
164
|
+
const result = await appendReactorSideEffects(eventLog, new SomethingHappened('a'), 'triggering-source', 'Default', 'triggering-source');
|
|
165
|
+
|
|
166
|
+
expect(appendMany).toHaveBeenCalledTimes(1);
|
|
167
|
+
expect(result.isSuccess).toBe(false);
|
|
168
|
+
expect(result.errors).toEqual(['Value must be unique']);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('when the handler returns a plain object that is not a known event type or EventForEventSourceId', () => {
|
|
173
|
+
const { eventLog, appendMany } = createEventLog();
|
|
174
|
+
|
|
175
|
+
it('should ignore it and not append anything', async () => {
|
|
176
|
+
const result = await appendReactorSideEffects(eventLog, { some: 'value' }, 'triggering-source', 'Default', 'triggering-source');
|
|
177
|
+
|
|
178
|
+
expect(appendMany).not.toHaveBeenCalled();
|
|
179
|
+
expect(result.isSuccess).toBe(true);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { hasEventType } from '../events/eventTypeDecorator';
|
|
5
|
+
import type { EventForEventSourceId } from '../eventSequences/EventForEventSourceId';
|
|
6
|
+
import type { IEventLog } from '../eventSequences/IEventLog';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Represents the outcome of appending a reactor handler's returned side-effect events.
|
|
10
|
+
*/
|
|
11
|
+
export interface ReactorSideEffectResult {
|
|
12
|
+
/** Whether every side-effect event appended successfully. */
|
|
13
|
+
readonly isSuccess: boolean;
|
|
14
|
+
|
|
15
|
+
/** Constraint violation and error messages, populated when {@link isSuccess} is false. */
|
|
16
|
+
readonly errors: string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const noSideEffects: ReactorSideEffectResult = { isSuccess: true, errors: [] };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Determines whether a value is shaped like an {@link EventForEventSourceId} — a plain object
|
|
23
|
+
* carrying its own `eventSourceId` and `event` — rather than an event object returned directly.
|
|
24
|
+
*/
|
|
25
|
+
function isEventForEventSourceId(value: unknown): value is EventForEventSourceId {
|
|
26
|
+
if (typeof value !== 'object' || value === null) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const candidate = value as Record<string, unknown>;
|
|
31
|
+
return typeof candidate.eventSourceId === 'string' &&
|
|
32
|
+
typeof candidate.event === 'object' && candidate.event !== null &&
|
|
33
|
+
hasEventType((candidate.event as object).constructor as Function);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Appends the events a `@reactor`-decorated class's handler method returned as side effects, in
|
|
38
|
+
* a single atomic `AppendMany` call — never one append per item, which would neither be atomic
|
|
39
|
+
* nor efficient.
|
|
40
|
+
* @param eventLog - The {@link IEventLog} to append the side effects to.
|
|
41
|
+
* @param handlerResult - Whatever the reactor handler method returned: `undefined`/`void`, a
|
|
42
|
+
* single event object, an array of events, a single {@link EventForEventSourceId}, an array of
|
|
43
|
+
* them, or a mix of bare events and {@link EventForEventSourceId} entries.
|
|
44
|
+
* @param triggeringEventSourceId - The event source id of the event that triggered the reactor,
|
|
45
|
+
* used as the target for bare event returns.
|
|
46
|
+
* @param triggeringEventStreamType - The event stream type of the triggering event, used for bare
|
|
47
|
+
* event returns.
|
|
48
|
+
* @param triggeringEventStreamId - The event stream identifier of the triggering event, used for
|
|
49
|
+
* bare event returns.
|
|
50
|
+
* @returns The outcome of the append. Each {@link EventForEventSourceId} entry keeps its own
|
|
51
|
+
* target (event source id, stream type/id, subject); bare events use the triggering event's.
|
|
52
|
+
*/
|
|
53
|
+
export async function appendReactorSideEffects(
|
|
54
|
+
eventLog: IEventLog,
|
|
55
|
+
handlerResult: unknown,
|
|
56
|
+
triggeringEventSourceId: string,
|
|
57
|
+
triggeringEventStreamType: string,
|
|
58
|
+
triggeringEventStreamId: string
|
|
59
|
+
): Promise<ReactorSideEffectResult> {
|
|
60
|
+
if (handlerResult === undefined || handlerResult === null) {
|
|
61
|
+
return noSideEffects;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const items = Array.isArray(handlerResult) ? handlerResult : [handlerResult];
|
|
65
|
+
const events: EventForEventSourceId[] = [];
|
|
66
|
+
|
|
67
|
+
for (const item of items) {
|
|
68
|
+
if (isEventForEventSourceId(item)) {
|
|
69
|
+
events.push(item);
|
|
70
|
+
} else if (typeof item === 'object' && item !== null && hasEventType(item.constructor as Function)) {
|
|
71
|
+
events.push({
|
|
72
|
+
eventSourceId: triggeringEventSourceId,
|
|
73
|
+
event: item,
|
|
74
|
+
eventStreamType: triggeringEventStreamType,
|
|
75
|
+
eventStreamId: triggeringEventStreamId,
|
|
76
|
+
subject: triggeringEventSourceId
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
// Anything else — undefined array entries, plain objects that are neither a registered
|
|
80
|
+
// event type nor EventForEventSourceId-shaped — is not a recognized side-effect and is
|
|
81
|
+
// silently ignored, the same as a handler that returns Task/void.
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (events.length === 0) {
|
|
85
|
+
return noSideEffects;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const results = await eventLog.appendMany(events);
|
|
89
|
+
const errors = results
|
|
90
|
+
.filter(result => !result.isSuccess)
|
|
91
|
+
.flatMap(result => [
|
|
92
|
+
...result.constraintViolations.map(violation => violation.message),
|
|
93
|
+
...result.errors.map(error => error.message)
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
return { isSuccess: errors.length === 0, errors };
|
|
97
|
+
}
|