Guides
SynapEvent Schema Reference
SynapEvent Schema Reference
Version: v1
Last Updated: 2025-12-04
Overview
All events in the Synap system must conform to the SynapEvent schema. This schema is defined using Zod and provides runtime validation for all event data.
Location: packages/types/src/synap-event.ts
Required Fields
| Field | Type | Description | Example |
|---|---|---|---|
id | string (UUID) | Unique event identifier | "123e4567-e89b-12d3-a456-426614174000" |
version | 'v1' (literal) | Schema version | 'v1' ⚠️ Not 1 |
type | string | Event type descriptor | 'note.created', 'entity.updated' |
data | Record<string, unknown> | Event payload | { content: "..." } |
userId | string | User who triggered event | "user-123" |
source | EventSource (enum) | Event origin | 'api' |
timestamp | Date | When event occurred | new Date() |
Optional Fields
| Field | Type | Description | Example |
|---|---|---|---|
subjectId | string (UUID) | Related aggregate ID | "entity-456" |
correlationId | string (UUID) | Request correlation | "req-789" |
causationId | string (UUID) | Causing event ID | "event-012" |
requestId | string (UUID) | HTTP request ID | "req-345" |
EventSource Enum
The source field must be one of these values:
| Value | Description | When to Use |
|---|---|---|
'api' | From API requests | HTTP/REST/tRPC endpoints |
'automation' | From automated processes | Scheduled tasks, triggers |
'sync' | From synchronization | Data sync operations |
'migration' | From data migration | Schema migrations, imports |
'system' | From system operations | Internal system events |
Generated Event Pattern
All table events follow the {table}.{action}.{modifier} pattern:
See: Event Types Catalog for all 55 event types
Creating Events
Basic Example
With Optional Fields
Common Mistakes
❌ Wrong: Number Version
✅ Correct: String Literal
❌ Wrong: Invalid Source
✅ Correct: Valid Enum Value
❌ Wrong: Missing Required Fields
✅ Correct: All Required Fields
Validation
Events are validated at runtime using Zod:
Storage Considerations
Date Serialization
When storing events in PostgreSQL via postgres.js:
EventRepository Usage
The EventRepository handles serialization automatically:
Testing Events
Creating Test Events
Type Safety
TypeScript provides full type safety:
Schema Definition
Full Zod schema (for reference):
Best Practices
1. Always Use Type Annotation
2. Generate UUIDs Properly
3. Use Current Timestamp
4. Meaningful Event Types
5. Structured Data Payload
References
- Schema Definition:
packages/types/src/synap-event.ts - Repository:
packages/database/src/repositories/event-repository.ts - Tests:
packages/database/src/__tests__/event-repository.test.ts - Error Report: See
error_resolution_report.mdfor common pitfalls
Need Help?
Common Issues:
- Version as number → Use
'v1'(string literal) - Invalid source → Check enum values above
- Missing fields → Ensure all required fields present
- Date serialization → Use EventRepository or convert to ISO string
Further Reading:
- Event Sourcing patterns
- CQRS architecture
- Database testing guide
