Architecture
Data Layer Overview

Source: data_layer/README.md

Data Layer Architecture

Overview

This data layer provides a unified schema management system for AltSportsData, with version control and multi-format generation.

Structure

data_layer/
β”œβ”€β”€ output_styles/
β”‚   └── schemas/
β”‚       └── domains/
β”‚           β”œβ”€β”€ v1/                          ← Version 1 (Current)
β”‚           β”‚   β”œβ”€β”€ python/                  ← SOURCE OF TRUTH (Canonical)
β”‚           β”‚   β”‚   β”œβ”€β”€ base/               # Core models
β”‚           β”‚   β”‚   β”œβ”€β”€ sports/             # Sports domain
β”‚           β”‚   β”‚   β”œβ”€β”€ leagues/            # League domain
β”‚           β”‚   β”‚   β”œβ”€β”€ business/           # Business domain
β”‚           β”‚   β”‚   β”œβ”€β”€ api/                # API models
β”‚           β”‚   β”‚   β”œβ”€β”€ workflows/          # Workflows
β”‚           β”‚   β”‚   β”œβ”€β”€ ml/                 # ML models
β”‚           β”‚   β”‚   β”œβ”€β”€ generators/         # Schema generators
β”‚           β”‚   β”‚   └── tests/              # Tests
β”‚           β”‚   β”‚
β”‚           β”‚   β”œβ”€β”€ typescript/             ← GENERATED from python/
β”‚           β”‚   β”œβ”€β”€ json/                   ← Existing JSON schemas
β”‚           β”‚   β”œβ”€β”€ drizzle/                ← Existing Drizzle ORM
β”‚           β”‚   β”œβ”€β”€ graphql/                ← Can generate from python/
β”‚           β”‚   β”œβ”€β”€ neo4j/                  ← Existing Neo4j schemas
β”‚           β”‚   └── converters/             ← Schema converters
β”‚           β”‚
β”‚           └── v2/                          ← Version 2 (Future)
β”‚               β”œβ”€β”€ python/                  # V2 models (when needed)
β”‚               β”œβ”€β”€ typescript/              # Generated TypeScript
β”‚               └── ...

Single Source of Truth

v1/python/ is the canonical schema source:

  • All models defined in Python/Pydantic
  • Other formats generated automatically
  • Version controlled alongside code

Generation Workflow

1. Define Models (Python/Pydantic)

# v1/python/leagues/questionnaires.py
from pydantic import Field
from ..base import BaseModel
 
class LeagueQuestionnaire(BaseModel):
    league_name: str = Field(..., description="League name")
    # ... more fields

2. Generate to Any Format

cd data_layer/output_styles/schemas/domains/v1/python
 
# Generate TypeScript to v1
python generate_schemas.py --format typescript --output ../typescript/
 
# Generate SQL to v1
python generate_schemas.py --format sql --output ../sql/
 
# Generate GraphQL to v1
python generate_schemas.py --format graphql --output ../graphql/
 
# Generate all formats
python generate_schemas.py --all

3. Use Generated Schemas

Backend (Python):

from apps.backend.core.models import LeagueQuestionnaire
# Imports from v1/python automatically

Frontend (TypeScript):

import { LeagueQuestionnaire } from '@/types/generated/leagues';
// Uses generated TypeScript from v1/typescript/

Version Management

v1 (Current - Production)

  • Stable API
  • Used by all current applications
  • Generate from v1/python/

v2 (Future - Next Generation)

  • Breaking changes
  • New features
  • Gradual migration path

Migration Path

v1/python/                  v2/python/
    ↓                          ↓
v1/typescript/   β†’  β†’  β†’   v2/typescript/
    ↓                          ↓
Frontend v1              Frontend v2

Generator Outputs

From v1/python/, you can generate:

FormatOutput LocationStatus
TypeScript + Zodv1/typescript/βœ“ Working
JSON Schemav1/json/Ready
SQL (PostgreSQL)v1/sql/Stub
GraphQLv1/graphql/Stub
Drizzle ORMv1/drizzle/Can integrate
Prismav1/prisma/Future

Integration Points

Backend Integration

# apps/backend/core/models/__init__.py
from data_layer.output_styles.schemas.domains.v1.python.base import BaseModel

Frontend Integration

// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/types/generated/*": ["./data_layer/output_styles/schemas/domains/v1/typescript/*"]
    }
  }
}

Database Integration

// Use generated Drizzle schemas
import { leagueTable } from '@/data_layer/schemas/domains/v1/drizzle/leagues';

Best Practices

1. Always Define in Python First

# βœ“ DO: Define in v1/python/
class NewModel(BaseModel):
    field: str
 
# βœ— DON'T: Manually create TypeScript
interface NewModel { ... }  // Will be overwritten by generator

2. Generate After Changes

# After modifying Python models
python generate_schemas.py --all

3. Version Breaking Changes

# For breaking changes, use v2
cp -r v1/python/ v2/python/
# Make breaking changes in v2/python/
# Generate v2 schemas separately

4. Test Generated Schemas

# Test Python models
pytest v1/python/tests/
 
# Test generated TypeScript
npm test

Quick Commands

# Navigate to Python schemas
cd data_layer/output_styles/schemas/domains/v1/python
 
# Preview migration from old models
python migrate_models.py --dry-run
 
# Execute migration
python migrate_models.py
 
# Generate all schemas
python generate_schemas.py --all
 
# Test everything
python quick_test.py
 
# Generate specific format
python generate_schemas.py --format typescript
python generate_schemas.py --format sql
python generate_schemas.py --format graphql

Benefits

βœ“ Single Source of Truth - Define once in Python βœ“ Version Control - v1, v2, etc. for breaking changes βœ“ Auto-Generation - TypeScript, SQL, GraphQL from Python βœ“ Type Safety - Strong typing everywhere βœ“ No Drift - Frontend/backend always in sync βœ“ Easy Migration - Clear path from v1 to v2

Next Steps

  1. Migrate existing models from apps/backend/core/models_backup/:

    cd v1/python
    python migrate_models.py --dry-run  # Preview
    python migrate_models.py            # Execute
  2. Generate all formats:

    python generate_schemas.py --all
  3. Update imports in backend/frontend to use generated schemas

  4. Set up CI/CD to auto-generate schemas on commit

Platform

Documentation

Community

Support

partnership@altsportsdata.comdev@altsportsleagues.ai

2025 Β© AltSportsLeagues.ai. Powered by AI-driven sports business intelligence.

πŸ€– AI-Enhancedβ€’πŸ“Š Data-Drivenβ€’βš‘ Real-Time