Skip to content

Commit 8dbee71

Browse files
update readme
1 parent 9241d53 commit 8dbee71

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ await db.commit(); // 💥 Fails! Message is in queue but no user record
4848

4949
**The Transactional Outbox Pattern** solves this by storing both the business data and events in a single database transaction, then processing events asynchronously with guaranteed delivery.
5050

51+
```typescript
52+
// ✅ Solution: Save both user and event in the same transaction
53+
await db.query("BEGIN");
54+
55+
// Save your business data
56+
await db.query("INSERT INTO users (id, email, name) VALUES ($1, $2, $3)", [
57+
userId,
58+
email,
59+
name,
60+
]);
61+
62+
// Save the event in the SAME transaction
63+
await db.query(
64+
"INSERT INTO events (id, type, data, correlation_id, handler_results, errors) VALUES ($1, $2, $3, $4, $5, $6)",
65+
[randomUUID(), "UserCreated", { userId, email }, correlationId, {}, 0],
66+
);
67+
68+
await db.query("COMMIT");
69+
// ✅ Both user and event are saved atomically!
70+
// If commit fails, neither is saved. If it succeeds, both are saved.
71+
// The processor will pick up the event and send the email (and any other side effects that you register) asynchronously
72+
```
73+
5174
## Features
5275

5376
-**At-least-once delivery** - Events are never lost, even during failures or crashes
@@ -107,13 +130,21 @@ await client.connect();
107130

108131
const processor = EventProcessor(createProcessorClient(client), {
109132
UserCreated: {
133+
// Handlers are processed concurrently and independently with retries
134+
// If one handler fails, others continue processing
110135
sendWelcomeEmail: async (event, { signal }) => {
111136
await emailService.send({
112137
to: event.data.email,
113138
subject: "Welcome!",
114139
template: "welcome",
115140
});
116141
},
142+
createStripeCustomer: async (event, { signal }) => {
143+
await stripe.customers.create({
144+
email: event.data.email,
145+
metadata: { userId: event.data.userId },
146+
});
147+
},
117148
},
118149
});
119150

0 commit comments

Comments
 (0)