Configuration Example

A comprehensive demonstration of CQL's configuration system, showing how to set up database connections, environment-specific settings, and advanced configuration options.

🎯 What You'll Learn

This example teaches you how to:

  • Configure CQL with different database adapters

  • Set environment-specific configurations

  • Manage connection pooling for optimal performance

  • Configure SSL/TLS for secure connections

  • Set up performance monitoring with custom settings

  • Validate configuration with custom validators

  • Use configuration helpers for common operations

  • Integrate configuration with schema definitions

🚀 Quick Start

# Run the configuration example
crystal examples/configure_example.cr

📁 Code Structure

🔧 Key Features

1. Basic Configuration

2. Environment-Specific Configuration

3. Advanced Configuration with Performance Monitoring

📊 Configuration Examples

Database-Specific Configuration

PostgreSQL with SSL

SQLite Performance Configuration

MySQL Configuration

Connection Pooling Configuration

Custom Validation

🏗️ Configuration Architecture

🔧 Configuration Options

Core Settings

Option
Type
Default
Description

database_url

String

"sqlite3://./db/development.db"

Database connection URL

logger

Log

Environment-based

Logger instance

default_timezone

Symbol

:utc

Default timezone

environment

String

ENV["CRYSTAL_ENV"]

Application environment

Migration Settings

Option
Type
Default
Description

migration_table_name

Symbol

:cql_schema_migrations

Migration tracking table

schema_path

String

"src/schemas"

Schema file path

schema_file_name

String

"app_schema.cr"

Schema file name

enable_auto_schema_sync

Bool

true

Auto schema synchronization

Performance Settings

Option
Type
Default
Description

enable_query_cache

Bool

false

Enable query caching

cache_ttl

Time::Span

1.hour

Cache time-to-live

enable_performance_monitoring

Bool

false

Enable performance monitoring

Connection Pool Settings

Option
Type
Default
Description

size

Int32

10

Maximum pool size

initial_size

Int32

1

Initial pool size

max_idle_size

Int32

1

Maximum idle connections

checkout_timeout

Time::Span

10.seconds

Connection checkout timeout

🎯 Environment-Specific Patterns

Development Environment

Test Environment

Production Environment

🔧 Configuration Helpers

Using Configuration Helpers

Schema Integration

🧪 Testing Configuration

Reset Configuration for Testing

Thread-Safe Configuration Access

📊 Configuration Validation

Built-in Validation

Custom Validation

🎯 Best Practices

1. Configure Early

2. Use Environment Variables

3. Separate Configuration by Environment

📚 Next Steps

Advanced Topics

Production Considerations

  • Environment Variables - Secure configuration management

  • Connection Pooling - Optimize for production load

  • SSL Configuration - Secure database connections

  • Performance Monitoring - Production performance tracking

  • Validation - Ensure configuration correctness

🔧 Troubleshooting

Common Issues

  1. Configuration not applied - Ensure configuration happens before schema definition

  2. Environment detection issues - Check CRYSTAL_ENV environment variable

  3. Validation errors - Review configuration values and custom validators

  4. Thread safety issues - Configuration is thread-safe by default

Debug Configuration


🏁 Summary

This configuration example demonstrates:

  • Comprehensive configuration options for all CQL features

  • Environment-specific settings for different deployment stages

  • Database-specific optimizations for PostgreSQL, MySQL, and SQLite

  • Performance monitoring setup with custom configurations

  • Validation patterns for ensuring configuration correctness

  • Thread-safe configuration access for concurrent applications

  • Integration with schema definitions for seamless setup

Ready to configure your own CQL application? Start with the basic configuration and gradually add advanced features as needed! 🚀

Last updated

Was this helpful?