Skip to main content

Overview

The Engine is the main entry point for ChameleonDB. It loads the schema from the Schema Vault, manages database connections, and provides access to query and mutation APIs.

Initialization

NewEngine()

Creates a new engine instance with automatic schema loading from the vault.
Behavior:
  • Loads schema from .chameleon/state/schema.merged.cham (or configured path)
  • Verifies vault integrity before loading
  • Returns error if vault is not initialized or integrity check fails
  • Ready to use immediately after creation
Example:

Connection Management

Connect()

Establishes a connection to the PostgreSQL database.
context.Context
required
Context for the connection operation
ConnectorConfig
required
Database connection configuration
ConnectorConfig Fields:
string
default:"localhost"
PostgreSQL server hostname
int
default:"5432"
PostgreSQL server port
string
required
Database name
string
required
Database user
string
Database password
int32
default:"10"
Maximum number of connections in the pool
int32
default:"2"
Minimum number of connections in the pool
time.Duration
default:"5m"
Maximum time a connection can be idle
Example with connection string:

Close()

Closes the database connection pool.
Example:

Ping()

Verifies the database connection is alive.
Example:

IsConnected()

Returns true if the engine is connected to a database.

Configuration

ParseConnectionString()

Parses a PostgreSQL connection URL into a ConnectorConfig.
Supported formats:
  • postgresql://user:password@host:port/dbname
  • postgres://user:password@host:port/dbname
Example:

DefaultConfig()

Returns sensible default configuration values.
Returns:

Schema Access

GetSchema()

Returns the currently loaded schema.
Example:

Version()

Returns the ChameleonDB engine version.
Example:

Debug Mode

WithDebug()

Enables debug output for all operations.
Debug Levels:
DebugLevel
No debug output (default)
DebugLevel
Show generated SQL queries
DebugLevel
Show SQL + execution time + row counts
DebugLevel
Show SQL + EXPLAIN output (future)
Example:
Environment variable:

Error Handling

Common Errors

error
The Schema Vault hasn’t been initialized. Run chameleon init first.
error
Schema vault has been tampered with. Run chameleon verify to investigate.
error
Attempted to query/mutate without calling Connect() first.
Example error handling:

Complete Example

See Also