System Overview
ChameleonDB governs schemas at runtime through versioning, cryptographic integrity, and explicit operational modes.
Architecture Layers
The system is organized into five distinct layers:Each layer has a specific responsibility. Security and governance are built into the core, not bolted on.
Core Components
1. Rust Core (chameleon-core)
ParserTransforms
.cham source into an AST using LALRPOP grammar. Handles entities, fields, relations, type modifiers, default values, and backend annotations.
Type CheckerValidates the AST before runtime. Organized in three modules:
relations.rs- Entity references and foreign key consistencyconstraints.rs- Primary keys and annotation rulesmod.rs- Pipeline orchestration and error reporting
Rule-based optimization for query execution plans. Deterministic, no ML in v1.x. Status: ✅ Complete
2. Schema Vault
Purpose: Immutable, versioned schema storage with integrity guarantees. See the Schema Vault page for complete details. Structure:- ✅ Automatic version registration on every migration
- ✅ SHA256 hash verification before every operation
- ✅ Tamper detection (if hash mismatch → abort)
- ✅ Lineage tracking (parent versions)
- ✅ Complete audit trail (who, what, when)
3. Integrity Modes
Purpose: Ring-based access control for schema governance. See the Integrity Modes page for complete details.| Mode | Ring | Use Case | Schema Changes |
|---|---|---|---|
| readonly | R3 | Production (default) | ❌ Blocked |
| standard | R2 | Development teams | ✅ Controlled |
| privileged | R1 | DBAs | ✅ Direct (logged) |
| emergency | R0 | Incident recovery | ✅ No checks (audited) |
- Application code checks mode before operations
- Mode upgrades require password authentication
- Downgrades don’t require password
- All mode changes logged
4. Go Runtime (chameleon)
EngineEntry point for Go applications. Loads schemas from vault, verifies integrity, enforces modes, and exposes public API. Key change in v1.0:
Translates validated queries into backend-specific SQL. Handles field projection (
.Select()), eager loading (.Include()), and filters.
Connection Poolpgx-based PostgreSQL connection management with automatic reconnection and health checks. IdentityMap (NEW in v1.0)
Automatic object deduplication in memory. When querying with
.Include(), parent objects are deduplicated to save memory.
Example:
Comprehensive PostgreSQL error → ChameleonDB error mapping with clear messages and suggestions. Status: ✅ Complete (v1.0)
5. CLI Tools
Commands:| Command | Purpose | Status |
|---|---|---|
init | Initialize project + vault | ✅ v1.0 |
migrate | Generate & apply migrations | ✅ v1.0 |
validate | Validate schema syntax | ✅ v1.0 |
verify | Verify vault integrity | ✅ v1.0 |
status | Show vault + mode status | ✅ v1.0 |
journal schema | View version history | ✅ v1.0 |
config | Manage modes & settings | ✅ v1.0 |
introspect | DB → Schema generation | ✅ v1.0 |
6. FFI Boundary
Communication between Rust core and Go runtime via C ABI. How it works:- Schemas serialized to JSON in Rust
- Passed to Go via C strings
- Go deserializes and uses
- Memory managed explicitly (Rust allocates, Go frees)
Compilation & Validation Flow
Validation happens at compile time. Invalid schemas never reach the database.
Execution Flow
The complete flow from migration to database:Security Model
See SECURITY.md in the source for complete details. Layers:- OS Permissions - File access control (0700)
- Hash Integrity - SHA256 tamper detection
- Integrity Modes - Runtime access control
- Vault Enforcement - No schema bypass
- Audit Trail - Complete forensics
Design Decisions
Why Rust for Core?
- True lambdas and closures (essential for query API)
- Extreme type safety (catch errors at compile time)
- Operator overloading (natural query syntax)
- Excellent performance on parser hot paths
Why Go for Runtime?
- Simple concurrency (goroutines for connection pooling)
- Excellent PostgreSQL driver (pgx)
- Single-binary deployment
- Great debugging tools
Why FFI?
- Each language does what it does best
- Minimal overhead (~100ns)
- Future-proof (easy to add Node, Python, Java bindings)
Why Schema Vault?
- Treats schemas as first-class artifacts
- Immutability prevents silent drift
- Cryptographic integrity (SHA256)
- Complete audit trail for compliance
Why Integrity Modes?
- Explicit governance (not just config)
- Runtime enforcement (not optional)
- Password-protected escalation
- Compliance-ready out of the box
Performance Targets (v1.0)
| Operation | Target | Notes |
|---|---|---|
| Schema parse | < 10ms | One-time, cold start |
| Type check | < 5ms | Per schema validation |
| FFI call | < 100ns | Per boundary crossing |
| Hash verification | < 1ms | SHA256 computation |
| Query compilation | < 1ms | Per query |
| Query execution | DB-bound | Optimized SQL generation |
Performance overhead from governance is negligible compared to database I/O.
Component Status
| Component | Status | Version | Notes |
|---|---|---|---|
| Parser | ✅ Complete | v1.0 | LALRPOP, all types |
| Type Checker | ✅ Complete | v1.0 | Relations, constraints, cycles |
| Schema Vault | ✅ Complete | v1.0 | Versioning, hashing, audit |
| Integrity Modes | ✅ Complete | v1.0 | 4 modes, password-protected |
| FFI Layer | ✅ Complete | v1.0 | C ABI bridge |
| Query Builder | ✅ Complete | v1.0 | Filter, Include, Select |
| Mutations | ✅ Complete | v1.0 | Insert, Update, Delete |
| IdentityMap | ✅ Complete | v1.0 | Object deduplication |
| Connection Pool | ✅ Complete | v1.0 | pgx-based |
| Error Mapping | ✅ Complete | v1.0 | Comprehensive |
| Migration Gen | ✅ Complete | v1.0 | PostgreSQL DDL |
| Introspection | ✅ Complete | v1.0 | DB → Schema |
| Debug Mode | ✅ Complete | v1.0 | SQL visibility |
| CLI Tools | ✅ Complete | v1.0 | 8 commands |
| Backend Registry | ⏳ Planned | v2.0 | Multi-backend routing |
| Code Generator | ⏳ Planned | v1.1+ | Boilerplate generation |
| Query Optimizer | ⏳ Planned | v1.5+ | Rule-based optimization |
Project Structure
Testing
Test coverage:- Rust: 96 tests ✅
- Go: 188 tests ✅
- Integration: 30 tests ✅
- Total: 314 tests passing
- Parser tests (syntax, error handling)
- Type checker tests (relations, cycles, constraints)
- Vault tests (versioning, integrity, modes)
- Query tests (filters, includes, selects)
- Mutation tests (CRUD operations)
- Error mapping tests (PostgreSQL → ChameleonDB)
Future Architecture (v2.0+)
Planned features (not in v1.x):
- Multi-backend routing (PostgreSQL + DuckDB + Redis)
- ML-based query optimization
- Visual schema editor
- Distributed vault (multi-node)
- Advanced observability
v2.0 features are not part of the open-source v1.x releases.
Summary
ChameleonDB v1.0 provides:- ✅ Schema Vault - Versioned, hash-verified schemas
- ✅ Integrity Modes - Explicit runtime governance
- ✅ Type-safe queries - Validated before execution
- ✅ Complete audit trail - Who, what, when
- ✅ Zero-config security - Fail-safe defaults
- ✅ Production-ready - 314 tests passing
Next Steps
- Learn about the Schema Vault
- Understand Integrity Modes
- Explore the Schema Language