Skip to content

Commit 9775d0c

Browse files
committed
Unsuitable intersection of different types
- Event & CommandMetadata - State & CommandMetadata - ViewState & EventMetadata It is better not to do it. These types don't have much in common.
1 parent ad51604 commit 9775d0c

File tree

6 files changed

+62
-38
lines changed

6 files changed

+62
-38
lines changed

src/lib/application/eventsourcing-aggregate.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,18 @@ class EventRepositoryImpl
239239
}
240240

241241
async save(
242-
eList: readonly (Evt & CmdMetadata)[],
242+
eList: readonly Evt[],
243+
commandMetadata: CmdMetadata,
243244
versionProvider: (e: Evt) => Promise<Version | null>
244245
): Promise<readonly (Evt & Version & EvtMetadata)[]> {
245246
//mapping the Commands metadata into Events metadata !!!
246247
const savedEvents: readonly (Evt & Version & EvtMetadata)[] =
247248
await Promise.all(
248-
eList.map(async (e: Evt & CmdMetadata, index) => ({
249+
eList.map(async (e: Evt, index) => ({
249250
kind: e.kind,
250251
value: e.value,
251252
version: ((await versionProvider(e))?.version ?? 0) + index + 1,
252-
traceId: e.traceId,
253+
traceId: commandMetadata.traceId,
253254
}))
254255
);
255256
storage.concat(savedEvents);

src/lib/application/eventsourcing-aggregate.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ export interface IEventRepository<C, E, V, CM, EM> {
4949
/**
5050
* Save events
5151
*
52-
* @param events - list of Events with Command Metadata
52+
* @param events - list of Events
53+
* @param commandMetadata - Command Metadata of the command that initiated `events`
5354
* @param versionProvider - A provider for the Latest Event in this stream and its Version/Sequence
5455
* @return a list of newly saved Event(s) of type `E` with Version of type `V` and with Event Metadata of type `EM`
5556
*/
5657
readonly save: (
57-
events: readonly (E & CM)[],
58+
events: readonly E[],
59+
commandMetadata: CM,
5860
versionProvider: (e: E) => Promise<V | null>
5961
) => Promise<readonly (E & V & EM)[]>;
6062
}
@@ -231,20 +233,19 @@ export class EventSourcingAggregate<C, S, E, V, CM, EM>
231233
}
232234

233235
async save(
234-
events: readonly (E & CM)[],
236+
events: readonly E[],
237+
commandMetadata: CM,
235238
versionProvider: (e: E) => Promise<V | null>
236239
): Promise<readonly (E & V & EM)[]> {
237-
return this.eventRepository.save(events, versionProvider);
240+
return this.eventRepository.save(events, commandMetadata, versionProvider);
238241
}
239242

240243
async handle(command: C & CM): Promise<readonly (E & V & EM)[]> {
241244
const currentEvents = await this.eventRepository.fetch(command);
242245

243246
return this.eventRepository.save(
244-
this.computeNewEvents(currentEvents, command).map((evt) => ({
245-
...evt,
246-
...command,
247-
})),
247+
this.computeNewEvents(currentEvents, command),
248+
command,
248249
async () => currentEvents[currentEvents.length - 1]
249250
);
250251
}
@@ -288,25 +289,22 @@ export class EventSourcingOrchestratingAggregate<C, S, E, V, CM, EM>
288289
}
289290

290291
async save(
291-
events: readonly (E & CM)[],
292+
events: readonly E[],
293+
commandMetadata: CM,
292294
versionProvider: (e: E) => Promise<V | null>
293295
): Promise<readonly (E & V & EM)[]> {
294-
return this.eventRepository.save(events, versionProvider);
296+
return this.eventRepository.save(events, commandMetadata, versionProvider);
295297
}
296298

297299
async handle(command: C & CM): Promise<readonly (E & V & EM)[]> {
298300
const currentEvents = await this.eventRepository.fetch(command);
299301
return this.eventRepository.save(
300-
(
301-
await this.computeNewEvents(
302-
currentEvents,
303-
command,
304-
async (cmd: C & CM) => await this.eventRepository.fetch(cmd)
305-
)
306-
).map((event) => ({
307-
...event,
308-
...command,
309-
})),
302+
await this.computeNewEvents(
303+
currentEvents,
304+
command,
305+
async (cmd: C & CM) => await this.eventRepository.fetch(cmd)
306+
),
307+
command,
310308
this.versionProvider.bind(this)
311309
);
312310
}

src/lib/application/materialized-view.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ class ViewStateRepositoryImpl
133133
return storage;
134134
}
135135
async save(
136-
s: ViewState & EventMetadata,
136+
s: ViewState,
137+
_: EventMetadata,
137138
v: Version | null
138139
): Promise<ViewState & Version> {
139140
storage = {

src/lib/application/materialized-view.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ export interface IViewStateRepository<E, S, V, EM> {
3838
* Save state
3939
*
4040
* @param state - State and Event Metadata of type `S & EM`
41+
* @param eventMetadata - Event Metadata of type `EM`
4142
* @param version - State version of type `V | null`
4243
* @return newly saved State and Version of type `S` & `V`
4344
*/
44-
readonly save: (state: S & EM, version: V | null) => Promise<S & V>;
45+
readonly save: (
46+
state: S,
47+
eventMetadata: EM,
48+
version: V | null
49+
) => Promise<S & V>;
4550
}
4651

4752
/**
@@ -97,8 +102,8 @@ export class MaterializedView<S, E, V, EM>
97102
return this.viewStateRepository.fetch(event);
98103
}
99104

100-
async save(state: S & EM, version: V | null): Promise<S & V> {
101-
return this.viewStateRepository.save(state, version);
105+
async save(state: S, eventMetadata: EM, version: V | null): Promise<S & V> {
106+
return this.viewStateRepository.save(state, eventMetadata, version);
102107
}
103108

104109
async handle(event: E & EM): Promise<S & V> {
@@ -108,7 +113,8 @@ export class MaterializedView<S, E, V, EM>
108113
event
109114
);
110115
return this.viewStateRepository.save(
111-
{ ...(newState as S), ...(event as EM) },
116+
newState,
117+
event as EM,
112118
currentStateAndVersion as V
113119
);
114120
}

src/lib/application/statestored-aggregate.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class StateRepositoryImpl
247247
async fetch(_c: Cmd): Promise<(State & Version) | null> {
248248
return stateStorage;
249249
}
250-
async save(s: State & Cmd): Promise<State & Version> {
250+
async save(s: State, _: Cmd): Promise<State & Version> {
251251
stateStorage = {
252252
evenNumber: s.evenNumber,
253253
oddNumber: s.oddNumber,
@@ -269,12 +269,15 @@ class StateAndMetadataRepositoryImpl
269269
async fetch(_c: Cmd): Promise<(State & Version & StateMetadata) | null> {
270270
return stateAndMetadataStorage;
271271
}
272-
async save(s: State & CmdMetadata): Promise<State & Version & StateMetadata> {
272+
async save(
273+
s: State,
274+
cm: CmdMetadata
275+
): Promise<State & Version & StateMetadata> {
273276
stateAndMetadataStorage = {
274277
evenNumber: s.evenNumber,
275278
oddNumber: s.oddNumber,
276279
version: stateAndMetadataStorage.version + 1,
277-
traceId: s.traceId,
280+
traceId: cm.traceId,
278281
};
279282
return stateAndMetadataStorage;
280283
}

src/lib/application/statestored-aggregate.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,15 @@ export interface IStateRepository<C, S, V, CM, SM> {
4242
* You can update/save the item/state, but only if the `version` number in the storage has not changed.
4343
*
4444
* @param state - State with Command Metadata of type `S & CM`
45+
* @param commandMetadata - Command Metadata of the command that initiated the `state`
4546
* @param version - The current version of the state
4647
* @return newly saved State of type `S & V & SM`
4748
*/
48-
readonly save: (state: S & CM, version: V | null) => Promise<S & V & SM>;
49+
readonly save: (
50+
state: S,
51+
commandMetadata: CM,
52+
version: V | null
53+
) => Promise<S & V & SM>;
4954
}
5055

5156
/**
@@ -175,8 +180,12 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
175180
return this.stateRepository.fetch(command);
176181
}
177182

178-
async save(state: S & CM, version: V | null): Promise<S & V & SM> {
179-
return this.stateRepository.save(state, version);
183+
async save(
184+
state: S,
185+
commandMetadata: CM,
186+
version: V | null
187+
): Promise<S & V & SM> {
188+
return this.stateRepository.save(state, commandMetadata, version);
180189
}
181190

182191
async handle(command: C & CM): Promise<S & V & SM> {
@@ -186,7 +195,8 @@ export class StateStoredAggregate<C, S, E, V, CM, SM>
186195
command
187196
);
188197
return this.stateRepository.save(
189-
{ ...newState, ...(command as CM) },
198+
newState,
199+
command as CM,
190200
currentState as V
191201
);
192202
}
@@ -224,8 +234,12 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
224234
return this.stateRepository.fetch(command);
225235
}
226236

227-
async save(state: S & CM, version: V | null): Promise<S & V & SM> {
228-
return this.stateRepository.save(state, version);
237+
async save(
238+
state: S,
239+
commandMetadata: CM,
240+
version: V | null
241+
): Promise<S & V & SM> {
242+
return this.stateRepository.save(state, commandMetadata, version);
229243
}
230244

231245
async handle(command: C & CM): Promise<S & V & SM> {
@@ -235,7 +249,8 @@ export class StateStoredOrchestratingAggregate<C, S, E, V, CM, SM>
235249
command
236250
);
237251
return this.stateRepository.save(
238-
{ ...newState, ...(command as CM) },
252+
newState,
253+
command as CM,
239254
currentState as V
240255
);
241256
}

0 commit comments

Comments
 (0)