class CQL::Repository(T, Pk)
Reference
< Object
The CQL::Repository
class provides a high-level interface for interacting with a specific table in the database. It includes methods for querying, creating, updating, deleting records, as well as pagination and counting.
Example: Creating a Repository
Methods
def all
Fetches all records from the table.
@return [Array(T)] The list of records.
Example:
def find(id : Pk)
Finds a record by primary key.
@param id [Pk] The primary key value.
@return [T | Nil] The record if found, or
nil
otherwise.
Example:
def create(attrs : Hash(Symbol, DB::Any))
Creates a new record with the specified attributes.
@param attrs [Hash(Symbol, DB::Any)] The attributes of the record.
@return [PrimaryKey] The ID of the created record.
Example:
def update(id : Pk, attrs : Hash(Symbol, DB::Any))
Updates a record by its ID with the given attributes.
@param id [Pk] The primary key value of the record.
@param attrs [Hash(Symbol, DB::Any)] The updated attributes.
@return [Nil]
Example:
def delete(id : Pk)
Deletes a record by its primary key.
@param id [Pk] The primary key value of the record.
@return [Nil]
Example:
def count
Counts all records in the table.
@return [Int64] The number of records.
Example:
def page(page_number : Int32, per_page = 10)
Fetches a paginated set of records.
@param page_number [Int32] The page number to fetch.
@param per_page [Int32] The number of records per page.
@return [Array(T)] The records for the requested page.
Example: