Skip to main content

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 @rspwnrs/event-schemas

Python

pip install rspwnrs-event-schemas

๐Ÿš€ Usage

TypeScript

import {
  UserCreatedEvent,
  UserUpdatedEvent,
  EventMetadata,
} from "@rspwnrs/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: "user-service",
  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="user-service",
    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="user-service",
        metadata=create_metadata(),
        data=user_data
    )
    # Send to Kafka...

๐Ÿ“‹ Available Types

Event Types

  • UserCreatedEvent - Emitted when a new user is created
  • UserUpdatedEvent - Emitted when a user is updated
  • UserDeletedEvent - Emitted when a user is deleted

Common Types

  • EventMetadata - Common metadata for all events
  • BaseEvent - Base event structure
  • UserPayload - User data payload
  • DeletedUserPayload - Payload for deleted user events

๐Ÿ”ง Development

Prerequisites

  • Node.js 20+
  • Python 3.8+

Setup

# Clone the repository
git clone https://github.com/rspwnrs/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 bump:versions

Schema Development

  1. Add new schemas in the schemas/ directory
  2. Follow naming conventions: Use kebab-case for file names
  3. Update dependencies: Add new schema files to the generation script
  4. 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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new schemas
  5. Submit a pull request

๐Ÿ“„ License

MIT License - see LICENSE file for details.

๐Ÿ”— Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rspwnrs_event_schemas-1.19.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rspwnrs_event_schemas-1.19.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file rspwnrs_event_schemas-1.19.0.tar.gz.

File metadata

  • Download URL: rspwnrs_event_schemas-1.19.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for rspwnrs_event_schemas-1.19.0.tar.gz
Algorithm Hash digest
SHA256 29515424d2af4fe812d4b18ea46de6ce2bfbd3ae76e940d0afce080cce5a6c06
MD5 0bcec4c1f64e939609d6720d8d2d33e4
BLAKE2b-256 8deeaceeff661d174314381f6ce6368846be2a3ca6ebea88045e1c82a6b25a03

See more details on using hashes here.

File details

Details for the file rspwnrs_event_schemas-1.19.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rspwnrs_event_schemas-1.19.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf10b393bd9ec8770b7f037f13f621c7637b365f44b72638cfb4504b99cc7fde
MD5 ec119f779d53771d4ba6c4f57a76f4e1
BLAKE2b-256 519ee320d1c4b7f3a7ecd68b24d9a0f1eb71d3c0e52128a0b63f134d4c0c0638

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page