Avro-based event schemas for TypeScript and Python services
Project description
Event Schemas
Avro-based event schemas for TypeScript and Python services
This repository contains Apache Avro schemas for event-driven communication between services, with auto-generated TypeScript and Python types.
๐ฆ Installation
TypeScript / JavaScript
npm install @godjigame/event-schemas
Python
pip install godjigame-event-schemas
๐ Usage
TypeScript
import { UserCreatedEvent, UserUpdatedEvent, EventMetadata } from '@godjigame/event-schemas';
// Create event metadata
const metadata: EventMetadata = {
correlationId: '123e4567-e89b-12d3-a456-426614174000',
causationId: '456e7890-e89b-12d3-a456-426614174001',
traceId: '789e1234-e89b-12d3-a456-426614174002'
};
// Create user created event
const userCreatedEvent: UserCreatedEvent = {
eventId: '550e8400-e29b-41d4-a716-446655440000',
eventType: 'user.created',
version: '1.0.0',
timestamp: new Date().toISOString(),
source: 'gamer-id',
metadata,
data: {
userId: 'user123',
email: 'user@example.com',
username: 'johndoe',
displayName: 'John Doe',
createdAt: new Date().toISOString(),
updatedAt: null
}
};
// Use in Kafka consumer
async function handleUserCreated(event: UserCreatedEvent) {
console.log(`User created: ${event.data.userId}`);
// Process event...
}
Python
from event_types import UserCreatedEvent, UserUpdatedEvent, EventMetadata
from datetime import datetime
import uuid
# Create event metadata
metadata = EventMetadata(
correlationId=str(uuid.uuid4()),
causationId=str(uuid.uuid4()),
traceId=str(uuid.uuid4())
)
# Create user created event
user_created_event = UserCreatedEvent(
eventId=str(uuid.uuid4()),
eventType="user.created",
version="1.0.0",
timestamp=datetime.utcnow().isoformat(),
source="gamer-id",
metadata=metadata,
data=UserPayload(
userId="user123",
email="user@example.com",
username="johndoe",
displayName="John Doe",
createdAt=datetime.utcnow().isoformat(),
updatedAt=None
)
)
# Use in Kafka producer
def publish_user_created(user_data):
event = UserCreatedEvent(
eventId=str(uuid.uuid4()),
eventType="user.created",
version="1.0.0",
timestamp=datetime.utcnow().isoformat(),
source="gamer-id",
metadata=create_metadata(),
data=user_data
)
# Send to Kafka...
๐ Available Types
Event Types
UserCreatedEvent- Emitted when a new user is createdUserUpdatedEvent- Emitted when a user is updatedUserDeletedEvent- Emitted when a user is deleted
Common Types
EventMetadata- Common metadata for all eventsBaseEvent- Base event structureUserPayload- User data payloadDeletedUserPayload- Payload for deleted user events
๐ง Development
Prerequisites
- Node.js 20+
- Python 3.8+
Setup
# Clone the repository
git clone https://github.com/goodgameteamit/event-schemas.git
cd event-schemas
# Install dependencies
npm install
# Generate types
npm run generate
Commands
# Generate TypeScript and Python types
npm run generate
# Validate schemas
npm run test:schemas
# Validate generated types
npm run test:types
# Run all tests
npm test
# Bump version
npm run version:bump
Schema Development
- Add new schemas in the
schemas/directory - Follow naming conventions: Use kebab-case for file names
- Update dependencies: Add new schema files to the generation script
- Test thoroughly: Run validation and generation after changes
Schema Evolution
When evolving schemas:
- โ Add new optional fields with default values
- โ Add new event types
- โ Update documentation
- โ Don't remove existing fields
- โ Don't rename existing fields
- โ Don't change field types
๐ Repository Structure
event-schemas/
โโโ schemas/ # Avro schema definitions
โ โโโ metadata.avsc
โ โโโ base-event.avsc
โ โโโ user-events.avsc
โโโ generated/ # Generated types
โ โโโ typescript/
โ โ โโโ index.ts
โ โโโ python/
โ โโโ __init__.py
โโโ scripts/ # Build scripts
โ โโโ generate-types.sh
โ โโโ validate-schemas.js
โโโ .github/workflows/ # CI/CD pipeline
โ โโโ release.yml
โโโ package.json # NPM package config
โโโ setup.py # Python package config
โโโ pyproject.toml # Modern Python config
๐ CI/CD Pipeline
The repository includes automated CI/CD with GitHub Actions:
- Pull Requests: Schema validation and type generation checks
- Main Branch: Automatic NPM publishing and continuous validation
Publishing
To publish a new version:
# Bump version in package.json and pyproject.toml
npm run version:bump
# Commit and push changes
git add package.json pyproject.toml
git commit -m "Bump version to x.x.x"
git push
๐ Schema Documentation
Event Metadata
All events include common metadata for tracing and correlation:
{
"correlationId": "Unique identifier for tracking related events",
"causationId": "Identifier of the event that caused this event",
"traceId": "Distributed tracing identifier"
}
Base Event Structure
All events extend the base event structure:
{
"eventId": "Unique identifier for this event",
"eventType": "Type of event (e.g., user.created)",
"version": "Schema version",
"timestamp": "ISO 8601 timestamp",
"source": "Service that generated the event",
"metadata": "Event metadata object",
"data": "Event-specific data"
}
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new schemas
- Submit a pull request
๐ License
MIT License - see LICENSE file for details.
๐ Related Projects
- Apache Avro - Data serialization system
- Confluent Schema Registry - Schema management
- Kafka - Event streaming platform
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file godjigame_event_schemas-1.11.0.tar.gz.
File metadata
- Download URL: godjigame_event_schemas-1.11.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
685d67be89b920887cfe8469f74c05c380c1b62258dfc3717bce0d56ef456e4a
|
|
| MD5 |
0ce98b6be98ac69388a71679c77d029a
|
|
| BLAKE2b-256 |
7258f1eb40d019d969b53bc7c4cfe899ad3e94701d3228b5d30f034f41f8250d
|
File details
Details for the file godjigame_event_schemas-1.11.0-py3-none-any.whl.
File metadata
- Download URL: godjigame_event_schemas-1.11.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1047416ccfcca4c9aae4aab01b61e206ad5906c0437285c05c2b179566e94408
|
|
| MD5 |
b7073830d14916292f0097658662e33d
|
|
| BLAKE2b-256 |
750104e52b42aeed7e7bcb2471eab502afb2c0063d91c5b5bfe6ac33543879c6
|