You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/docs/event-versioning.mdx
+94-56Lines changed: 94 additions & 56 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,42 @@ Once an event is in your event store, you can't change its structure. You need s
24
24
## Versioning Strategies
25
25
26
26
<Accordions>
27
-
<Accordiontitle="Strategy 1: Event Name Versioning">
27
+
<Accordiontitle="Strategy 1: Upcasting with migrate option">
28
+
Use the built-in `migrate` option in `createRepository` to transform old events before schema validation.
29
+
30
+
```typescript
31
+
const userRepository =createRepository(User, {
32
+
adapter,
33
+
migrate(rawEvent) {
34
+
// Rename legacy event name to the current one
35
+
if (rawEvent.eventName==="user:created") {
36
+
return {
37
+
...rawEvent,
38
+
eventName: "user:created_v2",
39
+
body: {
40
+
...rawEvent.body,
41
+
name: rawEvent.body.name||"Unknown", // Add missing field
42
+
},
43
+
};
44
+
}
45
+
returnrawEvent;
46
+
},
47
+
});
48
+
```
49
+
50
+
The `migrate` function runs on each raw event **before** schema validation, so only current event names need to be registered in the schema. Legacy event names can be removed from the schema entirely.
51
+
52
+
**Pros:**
53
+
- Single source of truth for migration logic
54
+
- No need to keep legacy schemas registered
55
+
- Centralized transformation logic
56
+
57
+
**Cons:**
58
+
- Performance overhead (transformation on every load)
59
+
- Can be complex with many migrations
60
+
</Accordion>
61
+
62
+
<Accordiontitle="Strategy 2: Event Name Versioning">
28
63
Create new event names for schema changes.
29
64
30
65
```typescript
@@ -75,52 +110,6 @@ Once an event is in your event store, you can't change its structure. You need s
0 commit comments