Models
CQL models are Crystal structs that map to database tables, providing type-safe CRUD operations.
Defining a Model
struct User
include CQL::ActiveRecord::Model(Int64)
db_context AppDB, :users
getter id : Int64?
getter name : String
getter email : String
getter active : Bool
getter created_at : Time
getter updated_at : Time
endKey components:
include CQL::ActiveRecord::Model(Int64)- Include model behavior with primary key typedb_context AppDB, :users- Bind model to schema and tablegetterproperties - Map to database columns
Primary Key Types
CRUD Operations
Create
Read
Update
Delete
Scopes
Define reusable query filters:
Callbacks
Execute code at specific points in the model lifecycle:
Available callbacks:
before_save,after_savebefore_create,after_createbefore_update,after_updatebefore_destroy,after_destroy
Using with Azu Endpoints
Model with All Features
Next Steps
Relationships - Define associations between models
Validations - Validate model data
Queries - Build complex database queries
Last updated
Was this helpful?
