Overview
The ChameleonDB schema language allows you to:- Define entities, fields, and relationships
- Specify types, constraints, and defaults
- Add backend annotations for future optimization
- Express intent clearly without SQL boilerplate
The schema language is compiled and validated before execution. Invalid schemas fail at compile time, not runtime.
Basic Syntax
Entities
Define domain entities using theentity keyword:
Fields
Each field has:- Name - Identifier (e.g.,
email) - Type - Data type (e.g.,
string,uuid,timestamp) - Modifiers - Optional constraints (e.g.,
primary,unique) - Defaults - Optional default values (e.g.,
default now())
Relationships
Express relationships directly in the schema:Type System
Primitive Types
| Type | Description | Example |
|---|---|---|
string | Text data | "John Doe" |
int | Integer numbers | 42 |
decimal | Decimal numbers | 99.99 |
bool | Boolean values | true, false |
uuid | UUID identifiers | 550e8400-e29b-41d4-a716-446655440000 |
timestamp | Date and time | 2026-02-20T15:45:00Z |
date | Date only | 2026-02-20 |
time | Time only | 15:45:00 |
Special Types
Type Validation
The type checker validates:- ✅ All field types are valid
- ✅ References to other entities exist
- ✅ Primary keys are defined
- ✅ Foreign keys match target types
- ✅ No circular ownership dependencies
Constraints
Primary Keys
Every entity must have exactly one primary key:Unique Constraints
Not Null
By default, all fields are NOT NULL unless marked optional:Default Values
Specify default values with thedefault keyword:
Supported Default Functions
now()- Current timestampuuid()- Generate UUID (backend-specific)- Literal values: strings, numbers, booleans
Relationships
One-to-Many
Many-to-One
One-to-One
Relationship validation happens at compile time. Invalid references fail before any code runs.
Annotations
Annotations provide semantic hints about data storage without changing the logical model:Available Annotations
| Annotation | Purpose | Example |
|---|---|---|
@cache | Cache-friendly data | Session tokens |
@olap | Analytical queries | Aggregates, metrics |
@vector | Vector search | ML embeddings |
Annotations are validated at compile time but currently act as declarative metadata. Future versions will use them for backend routing.
Complete Example
A full schema with multiple entities and relationships:Validation Pipeline
Schemas go through a three-stage validation pipeline:Invalid schemas fail at compile time with clear, contextual error messages.
Error Messages
ChameleonDB provides clear error messages:Schema Files
Schemas are stored in.cham files:
Multiple
.cham files are automatically merged during compilation.Compilation
Compile schemas with the CLI:Best Practices
- Use clear entity names -
User,Post, notusr,p - Always define primary keys - Usually
id: uuid primary - Make relationships explicit - Use
viafor clarity - Use meaningful defaults -
created_at: timestamp default now() - Annotate intentionally - Only when backend hints add value
- Keep schemas versioned - Commit
.chamfiles to git - Document complex relationships - Add comments for clarity
Schema Evolution
Schemas evolve through the Schema Vault:What ChameleonDB is NOT
The schema language is not:- ❌ A general-purpose programming language
- ❌ A query language (see Query API)
- ❌ SQL (though it generates SQL)
- ❌ A replacement for your database
Next Steps
- Learn about the Schema Vault for version management
- Understand Integrity Modes for governance
- Explore the Architecture