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
end

Key components:

  • include CQL::ActiveRecord::Model(Int64) - Include model behavior with primary key type

  • db_context AppDB, :users - Bind model to schema and table

  • getter properties - 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_save

  • before_create, after_create

  • before_update, after_update

  • before_destroy, after_destroy

Using with Azu Endpoints

Model with All Features

Next Steps

Last updated

Was this helpful?