CQL API

CQL (Crystal Query Language) is the ORM used with Azu for database operations.

Schema Definition

CQL::Schema.define

Create a database schema.

MyDB = CQL::Schema.define(
  :my_db,
  adapter: CQL::Adapter::SQLite,
  uri: "sqlite3://./db/development.db"
) do
  table :users do
    primary :id, Int64
    column :name, String
    column :email, String
    timestamps
  end
end

Parameters:

  • name : Symbol - Schema name

  • adapter : CQL::Adapter - Database adapter

  • uri : String - Connection string

Adapters

Available Adapters

Adapter
URI Format

CQL::Adapter::SQLite

sqlite3://./path/to/db.db

CQL::Adapter::Postgres

postgres://user:pass@host:5432/db

CQL::Adapter::MySql

mysql://user:pass@host:3306/db

Table Definition

table

Define a database table.

primary

Define primary key.

column

Define a column.

Column Types:

  • String - VARCHAR/TEXT

  • Int32, Int64 - INTEGER/BIGINT

  • Float32, Float64 - REAL/DOUBLE

  • Bool - BOOLEAN

  • Time - TIMESTAMP

  • JSON::Any - JSON

timestamps

Add created_at and updated_at columns.

foreign_key

Define a foreign key.

index

Define an index.

Model Definition

CQL::Model

Include in class to make it a model.

Parameters:

  • First type: Model class

  • Second type: Primary key type

db_context

Set database and table.

CRUD Operations

create

Create a new record.

find

Find by primary key.

find_by

Find by attributes.

save

Save record (insert or update).

update

Update attributes.

destroy

Delete record.

delete_all

Delete all matching records.

Query Methods

all

Get all records.

where

Filter records.

order

Sort records.

limit / offset

Paginate results.

count

Count records.

first / last

Get first or last record.

exists?

Check if records exist.

Associations

belongs_to

has_many

has_one

Callbacks

Available Callbacks

  • before_validation

  • after_validation

  • before_save

  • after_save

  • before_create

  • after_create

  • before_update

  • after_update

  • before_destroy

  • after_destroy

Scopes

Define reusable query scopes.

Transactions

Raw Queries

See Also

Last updated

Was this helpful?