Skip to main content
ChameleonDB includes a domain-specific language (DSL) for defining data models as semantic domains, not raw SQL tables. The language has its own syntax, parser, type system, and validation rules.

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 the entity 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

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:
Entities without a primary key will fail validation.

Unique Constraints

Not Null

By default, all fields are NOT NULL unless marked optional:

Default Values

Specify default values with the default keyword:

Supported Default Functions

  • now() - Current timestamp
  • uuid() - 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

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

  1. Use clear entity names - User, Post, not usr, p
  2. Always define primary keys - Usually id: uuid primary
  3. Make relationships explicit - Use via for clarity
  4. Use meaningful defaults - created_at: timestamp default now()
  5. Annotate intentionally - Only when backend hints add value
  6. Keep schemas versioned - Commit .cham files to git
  7. Document complex relationships - Add comments for clarity

Schema Evolution

Schemas evolve through the Schema Vault:
Each change creates a new immutable version in the 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
It is a domain-specific language for defining data models.

Next Steps