class Cql::Repository(T, Pk)
 
  - Cql::Repository(T, Pk)
- Reference
- Object
Overview
A repository for a specific table This class provides a high-level interface for interacting with a table It provides methods for querying, creating, updating, and deleting records It also provides methods for pagination and counting records
Example Creating a new repository
class UserRepository < Cql::Repository(User)
 def initialize(@schema : Schema, @table : Symbol)
end
user_repo = UserRepository.new(schema, :users)
user_repo.all
user_repo.find(1)Defined in:
repository.crConstructors
- 
        .new(schema : Schema, table : Symbol)
        
          Initialize the repository with a schema and table name 
Instance Method Summary
- 
        #all
        
          Fetch all records of type T - @return [Array(T)] The records 
- 
        #build(attrs : Hash(Symbol, DB::Any))
        
          Build a new object of type T with the given attributes - @param attrs [Hash(Symbol, DB::Any)] The attributes to use - @return [T] The new object 
- 
        #count
        
          Count all records in the table - @return [Int64] The number of records 
- 
        #create(attrs : Hash(Symbol, DB::Any))
        
          Create a new record with given attributes - @param attrs [Hash(Symbol, DB::Any)] The attributes to use - @return [PrimaryKey] The ID of the new record Example Creating a new record user_repo.create(name: "Alice", email: " [email protected]")
- #create(**fields)
- 
        #delete(id : Pk)
        
          Delete a record by ID - @param id [PrimaryKey] The ID of the record 
- #delete
- 
        #delete_all
        
          Delete all records in the table 
- 
        #delete_by(**fields)
        
          Delete records matching specific fields - @param fields [Hash(Symbol, DB::Any)] The fields to match 
- 
        #exists?(**fields)
        
          Check if records exist matching specific fields - @param fields [Hash(Symbol, DB::Any)] The fields to match - @return [Bool] True if records exist, false otherwise 
- 
        #find(id : Pk)
        
          Find a record by ID, return nil if not found - @param id [PrimaryKey] The ID of the record - @return [T?] The record, or nil if not found 
- 
        #find!(id : Pk)
        
          Find a record by ID, raise an error if not found - @param id [PrimaryKey] The ID of the record - @return [T] The record 
- 
        #find_all_by(**fields)
        
          Find all records matching specific fields - @param fields [Hash(Symbol, DB::Any)] The fields to match - @return [Array(T)] The records 
- 
        #find_by(**fields)
        
          Find a record by specific fields - @param fields [Hash(Symbol, DB::Any)] The fields to match - @return [T?] The record, or nil if not found 
- 
        #first
        
          Fetch the first record in the table - @return [T?] The first record, or nil if the table is empty 
- #insert
- 
        #last
        
          Fetch the last record in the table - @return [T?] The last record, or nil if the table is empty 
- 
        #page(page_number, per_page = 10)
        
          Paginate results based on page number and items per page - @param page_number [Int32] The page number to fetch - @param per_page [Int32] The number of items per page - @return [Array(T)] The records for the page 
- 
        #per_page(per_page)
        
          Limit the number of results per page - @param per_page [Int32] The number of items per page - @return [Array(T)] The records for the page 
- #query
- 
        #update(id : Pk, attrs : Hash(Symbol, DB::Any))
        
          Update a record by ID with given attributes - @param id [PrimaryKey] The ID of the record - @param attrs [Hash(Symbol, DB::Any)] The attributes to update 
- 
        #update(id : Pk, **fields)
        
          Update a record by ID with given fields - @param id [PrimaryKey] The ID of the record - @param fields [Hash(Symbol, DB::Any)] The fields to update 
- #update
- 
        #update_all(attrs : Hash(Symbol, DB::Any))
        
          Update all records with given attributes - @param attrs [Hash(Symbol, DB::Any)] The attributes to update 
- 
        #update_by(where_attrs : Hash(Symbol, DB::Any), update_attrs : Hash(Symbol, DB::Any))
        
          Update records matching where attributes with update attributes - @param where_attrs [Hash(Symbol, DB::Any)] The attributes to match - @param update_attrs [Hash(Symbol, DB::Any)] The attributes to update 
Constructor Detail
Initialize the repository with a schema and table name
- @param schema [Schema] The schema to use
- @param table [Symbol] The name of the table
- @return [Repository] The repository object
Example Creating a new repository
class UserRepository < Cql::Repository(User)
end
user_repo = UserRepository.new(schema, :usersInstance Method Detail
Fetch all records of type T
- @return [Array(T)] The records
Example Fetching all records
user_repo.allBuild a new object of type T with the given attributes
- @param attrs [Hash(Symbol, DB::Any)] The attributes to use
- @return [T] The new object
Example Building a new user object
user_repo.build(name: "Alice", email: " [email protected]")Count all records in the table
- @return [Int64] The number of records
Example Counting all records
user_repo.countCreate a new record with given attributes
- @param attrs [Hash(Symbol, DB::Any)] The attributes to use
- @return [PrimaryKey] The ID of the new record Example Creating a new record
user_repo.create(name: "Alice", email: " [email protected]")Delete a record by ID
- @param id [PrimaryKey] The ID of the record
Example Deleting a record by ID
user_repo.delete(1)Delete records matching specific fields
- @param fields [Hash(Symbol, DB::Any)] The fields to match
Example Deleting records by email
user_repo.delete_by(email: " [email protected]")Check if records exist matching specific fields
- @param fields [Hash(Symbol, DB::Any)] The fields to match
- @return [Bool] True if records exist, false otherwise
Example Checking if a record exists by email
user_repo.exists?(email: " [email protected]")Find a record by ID, return nil if not found
- @param id [PrimaryKey] The ID of the record
- @return [T?] The record, or nil if not found
Example Fetching a record by ID
user_repo.find(1)Find a record by ID, raise an error if not found
- @param id [PrimaryKey] The ID of the record
- @return [T] The record
Example Fetching a record by ID
user_repo.find!(1)Find all records matching specific fields
- @param fields [Hash(Symbol, DB::Any)] The fields to match
- @return [Array(T)] The records
Example Fetching all active users
user_repo.find_all_by(active: true)Find a record by specific fields
- @param fields [Hash(Symbol, DB::Any)] The fields to match
- @return [T?] The record, or nil if not found
Example Fetching a record by email
user_repo.find_by(email: " [email protected]")Fetch the first record in the table
- @return [T?] The first record, or nil if the table is empty
Example Fetching the first record
user_repo.firstFetch the last record in the table
- @return [T?] The last record, or nil if the table is empty
Example Fetching the last record
user_repo.lastPaginate results based on page number and items per page
- @param page_number [Int32] The page number to fetch
- @param per_page [Int32] The number of items per page
- @return [Array(T)] The records for the page
Example Paginating results
user_repo.page(1, 10)Limit the number of results per page
- @param per_page [Int32] The number of items per page
- @return [Array(T)] The records for the page
Example Limiting results per page
user_repo.per_page(10)Update a record by ID with given attributes
- @param id [PrimaryKey] The ID of the record
- @param attrs [Hash(Symbol, DB::Any)] The attributes to update
Example Updating a record by ID
user_repo.update(1, active: true)Update a record by ID with given fields
- @param id [PrimaryKey] The ID of the record
- @param fields [Hash(Symbol, DB::Any)] The fields to update
Example Updating a record by ID
user_repo.update(1, active: true)Update all records with given attributes
- @param attrs [Hash(Symbol, DB::Any)] The attributes to update
Example Updating all records
user_repo.update_all(active: true)Update records matching where attributes with update attributes
- @param where_attrs [Hash(Symbol, DB::Any)] The attributes to match
- @param update_attrs [Hash(Symbol, DB::Any)] The attributes to update
Example Updating records by email
user_repo.update_by(email: " [email protected]", active: true)