Architecture Overview
This guide provides a comprehensive overview of CQL's architecture, explaining how the different components work together to provide a powerful, type-safe ORM for Crystal applications.
🏗️ High-Level Architecture
CQL follows a layered architecture pattern that separates concerns and provides flexibility while maintaining performance:
🧩 Core Components
1. Schema Definition Layer
The schema layer is the foundation of CQL, providing type-safe database structure definition:
# Schema defines the database structure
AcmeDB = CQL::Schema.define(
:acme_db,
"postgresql://localhost/myapp",
CQL::Adapter::Postgres
) do
table :users do
primary :id, Int64
column :name, String
column :email, String
timestamps
end
endKey Responsibilities:
Database connection management
Table and column definitions
Index and constraint management
Migration execution
Transaction handling
2. Query Builder
The query builder provides a fluent interface for constructing SQL queries:
Features:
Type-safe query construction
Method chaining
Complex joins and subqueries
Aggregate functions
Raw SQL integration
3. Expression Builder
The expression builder translates Crystal expressions into SQL:
Capabilities:
Filter builders for where clauses
Having builders for aggregate conditions
Type-safe comparisons
Complex boolean logic
Aggregate function support
4. Database Adapters
Adapters handle database-specific SQL generation and features:
Adapter Features:
Database-specific SQL dialects
Connection pooling
Transaction management
Error handling
Feature detection
🎭 Design Patterns
Active Record Pattern
CQL implements the Active Record pattern where models represent database tables:
Pattern Flow:
Repository Pattern
For applications that prefer separation of concerns:
Pattern Flow:
🔄 Data Flow Architecture
Query Execution Flow
Model Lifecycle
🎯 Type Safety Architecture
Compile-Time Type Checking
CQL leverages Crystal's macro system for compile-time type safety:
Runtime Type Validation
🚀 Performance Architecture
Connection Management
Query Optimization
🔧 Extension Points
Custom Validators
Custom Column Types
🔍 Debugging and Introspection
Query Logging
Schema Introspection
📊 Performance Characteristics
Benchmarks
Simple SELECT
~50,000 ops/sec
Low
Complex JOIN
~10,000 ops/sec
Medium
Bulk Insert
~100,000 records/sec
Medium
Transaction
~20,000 ops/sec
Low
Optimization Tips
Use prepared statements - CQL automatically uses parameterized queries
Leverage connection pooling - Reuse database connections
Batch operations - Use transactions for multiple operations
Index wisely - Add indexes for frequently queried columns
Query optimization - Use
selectto limit returned columns
🎪 Advanced Patterns
Multi-Database Support
Migration Support
🛡️ Security Architecture
SQL Injection Prevention
Connection Security
🧭 Navigation
This architecture provides a solid foundation for building scalable, maintainable, and performant Crystal applications while maintaining type safety and developer productivity.
For more detailed information, see:
Last updated
Was this helpful?