@robinmalfait/event-source 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -3
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -163,7 +163,10 @@ Projectors build read models from events. They process events sequentially and m
|
|
|
163
163
|
This means projectors work well in serverless environments - projections are persisted to your database and don't need rebuilding on every cold start.
|
|
164
164
|
|
|
165
165
|
```typescript
|
|
166
|
-
import {
|
|
166
|
+
import {
|
|
167
|
+
Projector,
|
|
168
|
+
type ApplyProjectorEvents,
|
|
169
|
+
} from '@robinmalfait/event-source'
|
|
167
170
|
|
|
168
171
|
class BalanceProjector extends Projector {
|
|
169
172
|
name = 'balance-projector'
|
|
@@ -174,7 +177,7 @@ class BalanceProjector extends Projector {
|
|
|
174
177
|
this.balances.clear()
|
|
175
178
|
}
|
|
176
179
|
|
|
177
|
-
apply:
|
|
180
|
+
apply: ApplyProjectorEvents<typeof accountOpened | typeof moneyDeposited> = {
|
|
178
181
|
ACCOUNT_OPENED: (event, es) => {
|
|
179
182
|
this.balances.set(event.aggregateId, 0)
|
|
180
183
|
},
|
|
@@ -198,7 +201,7 @@ Each `apply` handler receives the `EventSource` instance as the second argument.
|
|
|
198
201
|
class TransactionHistoryProjector extends Projector {
|
|
199
202
|
name = 'transaction-history-projector'
|
|
200
203
|
|
|
201
|
-
apply:
|
|
204
|
+
apply: ApplyProjectorEvents<typeof accountClosed> = {
|
|
202
205
|
ACCOUNT_CLOSED: async (event, es) => {
|
|
203
206
|
// Reconstruct the aggregate to access its full state
|
|
204
207
|
let account = await es.load(new Account(), event.aggregateId)
|
|
@@ -272,6 +275,39 @@ if (balance < amount) {
|
|
|
272
275
|
|
|
273
276
|
### Type Utilities
|
|
274
277
|
|
|
278
|
+
#### ApplyEvents / ApplyProjectorEvents
|
|
279
|
+
|
|
280
|
+
Type-safe handlers for applying events to aggregates and projectors:
|
|
281
|
+
|
|
282
|
+
```typescript
|
|
283
|
+
import type {
|
|
284
|
+
ApplyEvents,
|
|
285
|
+
ApplyProjectorEvents,
|
|
286
|
+
} from '@robinmalfait/event-source'
|
|
287
|
+
|
|
288
|
+
// For Aggregates - handlers receive only the event
|
|
289
|
+
apply: ApplyEvents<typeof accountOpened | typeof moneyDeposited> = {
|
|
290
|
+
ACCOUNT_OPENED: (event) => {
|
|
291
|
+
/* ... */
|
|
292
|
+
},
|
|
293
|
+
MONEY_DEPOSITED: (event) => {
|
|
294
|
+
/* ... */
|
|
295
|
+
},
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// For Projectors - handlers receive the event and EventSource
|
|
299
|
+
apply: ApplyProjectorEvents<typeof accountOpened | typeof moneyDeposited> = {
|
|
300
|
+
ACCOUNT_OPENED: (event, es) => {
|
|
301
|
+
/* ... */
|
|
302
|
+
},
|
|
303
|
+
MONEY_DEPOSITED: (event, es) => {
|
|
304
|
+
/* ... */
|
|
305
|
+
},
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### PayloadOf / TypeOf
|
|
310
|
+
|
|
275
311
|
Extract types from events and commands:
|
|
276
312
|
|
|
277
313
|
```typescript
|
package/dist/index.d.cts
CHANGED
|
@@ -140,4 +140,4 @@ declare function createTestEventStore(commandHandlers: Record<string, CommandHan
|
|
|
140
140
|
then(expectation: EventType[] | Error): Promise<void>;
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
-
export { Aggregate, type ApplyEvents, Command, type CommandHandler, type CommandType, Event, type EventHandler, EventSource, type EventStore, type EventType, type MetadataEnhancer, type PayloadOf, Projector, type TypeOf, abort, createEventMapper, createTestEventStore, objectToYaml };
|
|
143
|
+
export { Aggregate, type ApplyEvents, type ApplyProjectorEvents, Command, type CommandHandler, type CommandType, Event, type EventHandler, EventSource, type EventStore, type EventType, type MetadataEnhancer, type PayloadOf, Projector, type TypeOf, abort, createEventMapper, createTestEventStore, objectToYaml };
|
package/dist/index.d.ts
CHANGED
|
@@ -140,4 +140,4 @@ declare function createTestEventStore(commandHandlers: Record<string, CommandHan
|
|
|
140
140
|
then(expectation: EventType[] | Error): Promise<void>;
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
-
export { Aggregate, type ApplyEvents, Command, type CommandHandler, type CommandType, Event, type EventHandler, EventSource, type EventStore, type EventType, type MetadataEnhancer, type PayloadOf, Projector, type TypeOf, abort, createEventMapper, createTestEventStore, objectToYaml };
|
|
143
|
+
export { Aggregate, type ApplyEvents, type ApplyProjectorEvents, Command, type CommandHandler, type CommandType, Event, type EventHandler, EventSource, type EventStore, type EventType, type MetadataEnhancer, type PayloadOf, Projector, type TypeOf, abort, createEventMapper, createTestEventStore, objectToYaml };
|
package/package.json
CHANGED