Aller au contenu principal

KytheraDB -- Architecture

Technical architecture reference for KytheraDB.


Table of Contents

  1. Project Structure
  2. Backend Architecture
  3. Frontend Architecture
  4. Database Models
  5. Service Layer
  6. API Route Structure
  7. Authentication
  8. Job Queue System
  9. Preflight Adapter Architecture
  10. Terminal Panel Architecture

Project Structure

auralith/
├── backend/
│ ├── app/
│ │ ├── main.py # FastAPI app, lifespan, router registration
│ │ ├── api/
│ │ │ ├── deps.py # Dependency injection (auth, plan, project access)
│ │ │ └── routes/ # All API route modules
│ │ │ ├── auth.py
│ │ │ ├── projects.py
│ │ │ ├── connections.py
│ │ │ ├── schemas.py
│ │ │ ├── crud.py
│ │ │ ├── graphs.py
│ │ │ ├── seeds.py
│ │ │ ├── qa_templates.py
│ │ │ ├── anonymization.py
│ │ │ ├── bridge.py
│ │ │ ├── export.py
│ │ │ ├── import_routes.py
│ │ │ ├── validation.py
│ │ │ ├── gdpr.py
│ │ │ ├── mass_patch.py
│ │ │ ├── sql_console.py
│ │ │ ├── performance.py
│ │ │ ├── preflight.py
│ │ │ ├── jobs.py
│ │ │ ├── project_config.py
│ │ │ ├── teams.py
│ │ │ ├── governance.py
│ │ │ ├── plans.py
│ │ │ ├── profiling.py
│ │ │ ├── activity.py
│ │ │ ├── references.py
│ │ │ ├── annotations.py
│ │ │ ├── playground.py
│ │ │ └── health.py
│ │ ├── core/
│ │ │ ├── config.py # Pydantic Settings (env vars)
│ │ │ ├── database.py # SQLAlchemy async engine and session
│ │ │ ├── security.py # JWT token creation/verification
│ │ │ └── encryption.py # Fernet encryption for connection credentials
│ │ ├── domain/
│ │ │ ├── models/ # SQLAlchemy ORM models
│ │ │ ├── services/ # Business logic layer
│ │ │ ├── schemas/ # Canonical schema types (introspection output)
│ │ │ ├── heuristics/ # Table role detection, domain patterns, enum detection
│ │ │ └── seeders/ # Seed data generation
│ │ │ ├── generators/ # Value generators (type-aware fake data)
│ │ │ ├── strategies/ # Seed strategies (standard, entity, QA)
│ │ │ └── planners/ # FK-aware seed plan builder
│ │ └── infrastructure/
│ │ ├── connectors/ # Database engine connectors
│ │ │ ├── base_connector.py # Abstract connector interface
│ │ │ ├── postgres_connector.py
│ │ │ ├── mysql_connector.py
│ │ │ ├── sqlite_connector.py
│ │ │ └── ssh_tunnel_connector.py
│ │ └── repositories/ # Data access layer
│ ├── requirements.txt
│ └── Dockerfile
├── frontend/
│ ├── src/
│ │ ├── app/
│ │ │ ├── app.routes.ts # Route definitions with guards
│ │ │ ├── core/
│ │ │ │ ├── services/ # Angular services (HTTP clients, state)
│ │ │ │ ├── guards/ # Route guards (auth, role, plan, project access)
│ │ │ │ └── models/ # TypeScript interfaces
│ │ │ ├── features/ # Feature modules (one per route)
│ │ │ │ ├── landing/
│ │ │ │ ├── auth/
│ │ │ │ ├── projects/
│ │ │ │ ├── dashboard/
│ │ │ │ ├── connections/
│ │ │ │ ├── schema-viewer/
│ │ │ │ ├── graph-viewer/
│ │ │ │ ├── data-explorer/
│ │ │ │ ├── seed-studio/
│ │ │ │ ├── anonymization/
│ │ │ │ ├── bridge/
│ │ │ │ ├── export/
│ │ │ │ ├── import/
│ │ │ │ ├── validation/
│ │ │ │ ├── gdpr/
│ │ │ │ ├── mass-patch/
│ │ │ │ ├── performance/
│ │ │ │ ├── preflight/
│ │ │ │ ├── profiling/
│ │ │ │ ├── activity/
│ │ │ │ ├── references/
│ │ │ │ ├── governance/
│ │ │ │ ├── teams/
│ │ │ │ ├── admin/
│ │ │ │ ├── settings/
│ │ │ │ ├── pricing/
│ │ │ │ └── terminal-detached/
│ │ │ └── shared/
│ │ │ └── components/
│ │ │ └── sidebar/ # Main navigation component
│ │ └── styles.css # Global design system tokens
│ ├── package.json
│ └── Dockerfile
├── docker-compose.yml
└── docs/

Backend Architecture

Framework and Patterns

The backend is built with FastAPI using the following architecture patterns:

  • Service Layer Pattern: All business logic lives in domain/services/. Routes are thin -- they validate input, call a service, and return the response.
  • Repository Pattern: Data access is handled through SQLAlchemy ORM models and the infrastructure/repositories/ layer.
  • Connector Pattern: Database engine connectivity is abstracted through infrastructure/connectors/ with a base connector interface.
  • Dependency Injection: FastAPI's Depends() system is used for database sessions, authentication, plan checks, and project access validation.

Key Technologies

ComponentTechnologyPurpose
Web frameworkFastAPI 0.115Async HTTP API with automatic OpenAPI docs
ORMSQLAlchemy 2.0Async ORM with asyncpg driver
DB driverasyncpg 0.30High-performance async PostgreSQL driver
MySQL driverPyMySQL 1.1MySQL/MariaDB connectivity
ValidationPydantic v2Request/response schema validation
AuthPyJWT 2.9JWT token generation and verification
Passwordsargon2-cffi 23.1Argon2id password hashing
SQL parsingsqlglot 30.1SQL analysis, dialect translation, linting
Fake dataFaker 30.0Realistic test data generation
Encryptioncryptography 43.0Fernet symmetric encryption for credentials
SSHsshtunnel 0.4SSH tunnel for remote database access
Settingspydantic-settingsEnvironment variable management

Request Flow

HTTP Request
→ FastAPI Router
→ Dependency Injection (get_db, get_current_user, require_plan, require_project_access)
→ Route Handler (thin controller)
→ Service Layer (business logic)
→ Connector / ORM (data access)
→ External Database or Platform DB

Frontend Architecture

Framework and Patterns

The frontend is built with Angular 21 using modern patterns:

  • Standalone Components: No NgModules. Every component is standalone with explicit imports.
  • Signals: Angular signals for reactive state management (replacing RxJS subjects for local state).
  • Lazy Loading: All feature routes use loadComponent() for code splitting.
  • Service-based State: Core services manage HTTP communication and shared state.

Key Technologies

ComponentTechnologyPurpose
FrameworkAngular 21SPA framework with signals and standalone
LanguageTypeScript 5.9Type-safe JavaScript
CSSTailwind CSS v4Utility-first CSS with custom theme tokens
SQL EditorCodeMirror 6SQL editing with syntax highlighting
GraphCytoscape.jsInteractive graph visualization
Graph layoutdagreHierarchical directed graph layout
HTTPAngular HttpClientAPI communication
RoutingAngular RouterSPA navigation with guards

Component Architecture

Features are organized by route, each in its own directory under features/:

features/
├── mass-patch/
│ ├── mass-patch.component.ts # List view
│ └── components/
│ ├── patch-form.component.ts # Create/edit
│ ├── patch-detail.component.ts # Detail view
│ ├── patch-builder.component.ts # Builder UI
│ ├── diff-viewer.component.ts # Before/after diff
│ ├── dry-run-panel.component.ts # Dry-run results
│ ├── guard-rails-panel.component.ts
│ ├── execution-panel.component.ts
│ ├── export-panel.component.ts
│ ├── sql-analysis-panel.component.ts
│ └── template-builder.component.ts

Route Guards

GuardPurpose
adminGuardRequires admin role
superAdminGuardRequires super_admin role
teamAdminGuardRequires admin role (team management)
notSuperAdminGuardBlocks super_admin from project routes
projectAccessGuardValidates team membership or direct permission

Database Models

All models use SQLAlchemy 2.0 declarative style and are registered in domain/models/__init__.py.

ModelTablePurpose
Userapp_usersPlatform users with email, password hash, role, plan, email_verified, account_type, stripe_customer_id
SubscriptionsubscriptionsStripe subscriptions: plan, billing_cycle, status, period dates
ProjectprojectsTop-level project container, optional team_id
ConnectionconnectionsDatabase connection configs (encrypted credentials)
SchemaSnapshotschema_snapshotsCaptured database schema state (JSON)
ReferenceDefinitionreference_definitionsEnum/reference value definitions per column
SeedHistoryseed_historiesRecord of seed operations with row counts
SeedScenarioseed_scenariosQA template definitions for repeatable seeds
AuditLogaudit_logsAction audit trail (user, action, timestamp)
ProjectMemberproject_membersUser-project membership
TeamteamsTeam groupings for governance
TeamMemberteam_membersUser-team membership with role
ValidationRulevalidation_rulesData quality rules (regex, not_null, unique, etc)
ValidationScanRunvalidation_scan_runsScan execution results with violations
AnnotationannotationsUser annotations on tables/columns
PatchFichepatch_fichesMass patch definitions (SQL, status, metadata)
PatchSnapshotpatch_snapshotsBefore/after data snapshots for patches
GuardRailPolicyguard_rail_policiesProject-level SQL execution policies
JobjobsAsync job queue entries (progress, status, result)
AnonymizationConfiganonymization_configsSaved anonymization configurations per project
ProjectConfigproject_configsProject settings (guard rails, environments, etc)
ProjectPermissionproject_permissionsPer-user project access grants (READ, WRITE, etc)

Service Layer

All services are located in backend/app/domain/services/.

ServiceResponsibility
auth_serviceUser registration, login, JWT verification, password reset
introspection_serviceDatabase schema introspection via connectors
crud_serviceDynamic CRUD operations on user databases
graph_serviceBuild graph representations of table relationships
seed_serviceStandard and entity-aware seed generation
qa_template_serviceQA template CRUD and execution
anonymization_serviceColumn anonymization with multiple strategies
bridge_serviceData transfer between connections
export_serviceData export (CSV, SQL, docs)
import_serviceData import (CSV, SQL, bulk)
validation_serviceValidation rule management and scan execution
gdpr_serviceCompliance scanning against regulation patterns
patch_serviceMass patch CRUD and workflow management
dry_run_servicePatch dry-run simulation in transaction
snapshot_serviceBefore/after data snapshot capture
execution_servicePatch execution with progress tracking
patch_execution_engineCore patch SQL execution engine
guard_rails_serviceGuard rail policy evaluation
sql_analysis_serviceSQL parsing and risk analysis
sql_console_serviceSQL execution, preview, EXPLAIN
sql_parserSQL statement parsing and classification
sql_playground_serviceLegacy SQL playground execution
performance_servicepg_stat_statements analysis, index recommendations
preflight_serviceSQL preflight analysis with multi-DB adapters
plan_servicePlan definitions, feature checks, limit validation
job_serviceAsync job queue management with SSE streaming
connection_serviceConnection CRUD and testing
reference_serviceReference data management
profiling_serviceColumn-level data profiling (distributions, stats)
health_serviceSchema health audit
domain_classifier_serviceTable domain classification
normalization_serviceSchema normalization
capability_servicePer-engine capability detection
migration_serviceMigration script generation
email_serviceAsync email sending via aiosmtplib (Mailpit in dev)
stripe_serviceStripe integration: products, subscriptions, invoices
confirm_service (frontend)Generic confirmation dialog service (Promise-based)
doc_serviceDocumentation generation (Markdown, HTML)
changeset_export_serviceLiquibase changeset XML export
regulations_registryRegulation definitions for compliance scanning
entity_view_service360-degree entity view builder
template_sql_generatorSQL generation from patch templates
snapshot_utilsSnapshot utility functions

API Route Structure

All routes are registered in main.py. Routes are grouped by functional module:

ModulePrefixAuth Required
HealthGET /healthNo
Auth/auth/*Varies
Projects/projects/*Yes
Connections/projects/{id}/connections/*Yes
Schemas/projects/{id}/schema/*Yes
Data (CRUD)/projects/{id}/data/*Yes
Graphs/projects/{id}/graph/*Yes
SQL Console/projects/{id}/sql/*Yes
Mass Patch/projects/{id}/patches/*Yes
Bridge/projects/{id}/bridge/*Yes
Anonymization/projects/{id}/anonymize/*Yes
Seeds/projects/{id}/seed/*Yes
QA Templates/projects/{id}/qa-templates/*Yes
Validation/projects/{id}/validation/*Yes
Performance/projects/{id}/performance/*Yes
Preflight/projects/{id}/preflight/*Yes
GDPR/projects/{id}/gdpr/*Yes
Export/projects/{id}/export/*Yes
Import/projects/{id}/import/*Yes
Jobs/projects/{id}/jobs/*Yes
Configuration/projects/{id}/config/*Yes
Profiling/projects/{id}/profiling/*Yes
Activity/projects/{id}/activity/*Yes
References/projects/{id}/references/*Yes
Annotations/projects/{id}/annotations/*Yes
Plans/plans/*Yes
Teams/teams/*Yes (admin)
Governance/governance/*Yes (super_admin)
Billing/billing/*Yes

New Auth Endpoints (v2)

EndpointMethodAuthPurpose
/auth/registerPOSTNoRegister with email verification
/auth/loginPOSTNoLogin (rejects unverified emails)
/auth/verify-emailPOSTNoVerify email with token
/auth/resend-verificationPOSTNoResend verification email
/auth/forgot-passwordPOSTNoRequest password reset
/auth/reset-passwordPOSTNoReset password with token

Billing Endpoints

EndpointMethodAuthPurpose
/billing/configGETNoStripe publishable key
/billing/subscribePOSTYesCreate subscription + return client_secret
/billing/syncPOSTYesSync subscription status from Stripe
/billing/cancelPOSTYesCancel subscription
/billing/subscriptionGETYesCurrent subscription info
/billing/invoicesGETYesInvoice history from Stripe
/billing/webhookPOSTNoStripe webhook handler

Authentication

Flow

  1. Registration: POST /auth/register -- creates user with Argon2id hashed password, returns JWT.
  2. Login: POST /auth/login -- verifies credentials, returns JWT.
  3. Token Verification: Every authenticated request includes Authorization: Bearer <token>. The get_current_user dependency extracts and verifies the JWT.

Dependencies

get_current_user # Extract JWT, return User or None
require_role(*roles) # Check user role (super_admin passes all)
require_plan(min_plan) # Check user plan meets minimum
require_project_access() # Check team membership or direct permission

Plan Hierarchy

starter (0) < pro (1) < enterprise (2)

Role Hierarchy

viewer < qa < dev < admin < super_admin

super_admin implicitly passes any role check but is explicitly blocked from project access routes (governance only).


Job Queue System

Long-running operations (mass patch execution, bulk imports, bridge transfers) use an async job queue with SSE (Server-Sent Events) streaming.

Architecture

  1. Job Creation: A route creates a Job record in the database with status pending.
  2. Background Execution: The operation runs asynchronously, updating Job.progress, Job.message, Job.status.
  3. SSE Streaming: GET /projects/{id}/jobs/{job_id}/stream opens an SSE connection that polls the job and emits progress events.
  4. Completion: When the job finishes, status is set to done (or error), and the SSE stream closes.
  5. Cancellation: POST /projects/{id}/jobs/{job_id}/cancel sets status to cancelled.

Job Statuses

pending → running → done
→ error
→ cancelled

Job Fields

FieldTypeDescription
idUUIDUnique job identifier
project_idUUIDAssociated project
user_idUUIDUser who initiated the job
typestringJob type (patch_execute, etc.)
labelstringHuman-readable description
statusstringCurrent status
progressfloat0.0 to 1.0
messagestringCurrent step description
resultJSONFinal result payload
errorstringError message if failed
created_attimestampCreation time
started_attimestampExecution start time
completed_attimestampCompletion time

Preflight Adapter Architecture

The Preflight Guard uses a multi-database adapter pattern to support different SQL dialects and database-specific analysis.

Design

PreflightService
├── PostgresAdapter
│ ├── parse_statement()
│ ├── estimate_rows()
│ └── assess_risk()
├── MySQLAdapter
│ ├── parse_statement()
│ ├── estimate_rows()
│ └── assess_risk()
└── SQLiteAdapter
├── parse_statement()
├── estimate_rows()
└── assess_risk()

Analysis Pipeline

  1. Parse: Extract statement type, affected tables, WHERE clause presence using sqlglot.
  2. Estimate: Run EXPLAIN (or equivalent) to estimate affected row count.
  3. Environment Check: Apply stricter rules for production-tagged connections.
  4. Risk Classification: Assign risk level (safe, low, medium, high, critical).
  5. Decision: Return proceed, warn, or block based on risk level and guard rail policies.

Terminal Panel Architecture

The SQL Console terminal supports four layout modes to accommodate different workflows:

Layout Modes

ModeDescription
pillCompact inline button that expands on click
overlayFloating panel over the current view
terminalFull-width panel anchored to the bottom of the viewport
detachedSeparate browser window (/terminal-detached route)

Detached Terminal

The detached mode opens a new browser window at /terminal-detached. Communication between the main window and the detached terminal uses browser postMessage API or shared state through the Angular service layer.

Editor Stack

CodeMirror 6 Editor
├── @codemirror/lang-sql # SQL language support
├── @codemirror/state # Editor state management
├── @codemirror/view # View layer
└── @codemirror/theme-one-dark # Dark theme