Working with Prisma Pulse
1. Setting Up Prisma Pulse
npm install @prisma/extension-pulse
| Requirement | Detail |
|---|---|
| PG logical replication | wal_level=logical |
| PDP project | Enable Pulse + API key |
2. Subscribing to Database Changes
import { PrismaClient } from "@prisma/client";
import { withPulse } from "@prisma/extension-pulse";
const prisma = new PrismaClient().$extends(withPulse({ apiKey: process.env.PULSE_API_KEY! }));
const sub = await prisma.user.stream();
for await (const event of sub) console.log(event.action, event.created);
| Action | Detail |
|---|---|
| create | event.created |
| update | event.before / after |
| delete | event.deleted |
3. Filtering Change Events
await prisma.user.stream({
create: { after: { role: "ADMIN" } }
});
| Filter | Detail |
|---|---|
| action-level | create / update / delete |
| field match | Server-side filter |
4. Implementing Real-Time Updates
| Use Case | Pattern |
|---|---|
| Live dashboard | WebSocket → client |
| Notifications | Push to subscribers |
| Cache invalidation | Trigger Accelerate purge |
5. Handling Event Streams
| Concept | Detail |
|---|---|
| Async iterator | for await (const evt of sub) |
| stop() | Close subscription |
| Backpressure | Process events FIFO |
6. Implementing Webhooks
| Pattern | Detail |
|---|---|
| Bridge | Pulse listener → HTTP POST |
| Sign | HMAC payload for integrity |
| Retry | Outbox + DLQ on failure |
7. Managing Subscriptions
| Operation | Detail |
|---|---|
| Create | per-model stream() |
| Resume | Use name option for durable resume |
| Stop | sub.stop() |
8. Filtering by Model Operations
| Option | Detail |
|---|---|
| create | Only inserts |
| update | Updates with before/after |
| delete | Deletes only |
9. Using Pulse with Events
| Integration | Detail |
|---|---|
| Kafka bridge | Publish change events as topic |
| EventEmitter | App-level pub/sub |
| tRPC subscriptions | Forward to clients |
10. Monitoring Pulse Activity
| Source | Detail |
|---|---|
| PDP dashboard | Events/sec, lag |
| App logs | Event volume per topic |
| Replication lag | DB-side metric |