Active Record with CQL
An overview of CQL's Active Record capabilities for defining models, interacting with your database, managing data integrity, and more.
Last updated
Was this helpful?
An overview of CQL's Active Record capabilities for defining models, interacting with your database, managing data integrity, and more.
Last updated
Was this helpful?
This guide provides a comprehensive overview of Crystal Query Language (CQL)'s Active Record implementation. Active Record is a design pattern that connects database tables to classes (or structs in Crystal), allowing you to interact with your data through objects and methods rather than raw SQL queries.
CQL's Active Record module offers a powerful and intuitive way to manage your database records, inspired by established ORMs while leveraging Crystal's type safety and performance.
This central README provides a high-level introduction. For in-depth information on specific aspects of CQL Active Record, please refer to the following guides:
: Initial configuration for using CQL and Active Record. (Covered below)
: Learn how to define your Active Record models, map them to database tables, specify primary keys, and work with attributes.
: Detailed guide on creating, reading, updating, and deleting records using Active Record methods.
: Explore the powerful query interface, including direct finders, chainable queries, aggregations, and scopes.
: Ensure data integrity by using database transactions for multi-step operations.
: Understand how to check if a record is persisted and how to reload its data from the database.
: Ensure data integrity by defining and using model validations.
: Hook into the lifecycle of your models to trigger logic at specific events (e.g., before save, after create).
: Define and use associations between models:
(covers has_and_belongs_to_many
)
: Manage your database schema changes over time.
: Define reusable query constraints for cleaner and more readable code.
: Easily paginate query results.
Before getting started, ensure you have the following:
Crystal language installed (latest stable version recommended).
A supported relational database (e.g., PostgreSQL, MySQL) set up and accessible.
CQL added to your Crystal project.
Include CQL in your project's shard.yml
:
Then, run shards install
to download and install the dependency.
You need to configure CQL to connect to your database. This is typically done by setting a database URL and opening a connection. You might also define a database context for your application.
Note: The exact mechanism for defining your database context (AcmeDB
in the example) and making it accessible to your models should align with CQL's specific API and your application structure. Refer to CQL's core documentation for advanced database connection management, pooling, and context configuration.
Models are Crystal structs
including CQL::ActiveRecord::Model(PkType)
and use db_context
to link to a table.
CQL provides intuitive methods for creating, reading, updating, and deleting records (e.g., save
, create!
, find?
, find_by!
, update!
, delete!
).
Fetch records using direct finders or build complex queries with a chainable interface (.where
, .order
, .limit
, etc.).
Maintain data integrity with ACID-compliant database transactions. CQL provides both model-level transaction support and a service objects pattern for complex operations.
Ensure data integrity with built-in or custom validation rules triggered before saving records.
Execute custom logic at different points in a model's lifecycle (e.g., before_save
, after_create
).
Define associations like belongs_to
, has_many
, has_one
, and many_to_many
to manage relationships between models.
Manage database schema changes systematically using Crystal-based migration files.
Create reusable query shortcuts to keep your code clean and expressive.
See the full for details on attributes, primary keys, and more.
Explore the for comprehensive examples.
Dive into the for all query-building capabilities.
Learn more in the for maintaining data integrity across multiple operations.
Learn more in the .
Consult the for usage details.
See the for how to write and run migrations.
Read the for defining and using scopes.