Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Represents a database primary key column type.
Cql::BaseColumn
< Reference
< Object
A column in a table This class represents a column in a table It provides methods for setting the column type, default value, and constraints It also provides methods for building expressions
Example Creating a new column
(name : Symbol, type : T.class, as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Index | Nil = nil)
Create a new column instance
@param : name (Symbol) - The name of the column
@param : type (Any) - The data type of the column
@param : as_name (String, nil) - An optional alias for the column
@param : null (Bool) - Whether the column allows null values (default: false)
@param : default (DB::Any) - The default value for the column (default: nil)
@param : unique (Bool) - Whether the column should have a unique constraint (default: false)
@param : size (Int32, nil) - The size of the column (default: nil)
@param : index (Index, nil) - The index for the column (default: nil)
@return : Nil
@raise : Cql::Error if the column type is not valid
Example
Expressions for this column
@return [Expression::ColumnBuilder] the column expression builder
Example
(value)
Validate the value
@param value [DB::Any] The value to validate
Example
Reference
< Object
A foreign key constraint This class represents a foreign key constraint It provides methods for setting the columns, table, and references It also provides methods for setting the on delete and on update actions
Example Creating a new foreign key
(name : Symbol, columns : Array(Symbol), table : Symbol, references : Array(Symbol), on_delete : String = "NO ACTION", on_update : String = "NO ACTION")
Reference
< Object
The Schema
class represents a database schema.
This class provides methods to build and manage a database schema, including creating tables, executing SQL statements, and generating queries.
Example Creating a new schema
Example Executing a SQL statement
Example Creating a new query
The Schema
class represents a database schema.
(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0")
Initializes a new schema.
@param name [Symbol] the name of the schema
@param uri [String] the URI of the database
@param adapter [Adapter] the database adapter (default: Adapter::Sqlite
)
@param version [String] the version of the schema (default: "1.0")
Example Initializing a new schema
(name : Symbol, uri : String, adapter : Adapter = Adapter::Sqlite, version : String = "1.0", &)
Builds a new schema.
@param name [Symbol] the name of the schema
@param uri [String] the URI of the database
@param adapter [Adapter] the database adapter (default: Adapter::Sqlite
)
@param version [String] the version of the schema (default: "1.0")
@yield [Schema] the schema being built
@return [Schema] the built schema
Example
@return [Adapter] the database adapter (default: Adapter::Sqlite
)
(table_name : Symbol, &)
Alter a table in the schema.
@param table_name [Symbol] the name of the table
@yield [AlterTable] the table being altered Example
Example
@return [DB::Connection] the database connection
Creates a new delete query for the schema.
@return [Delete] the new delete query Example
(sql : String)
Executes a SQL statement.
@param sql [String] the SQL statement to execute
Example
@return [Expression::Generator] the expression generator
Creates a new insert query for the schema.
@return [Insert] the new insert query Example
Creates a new migrator for the schema.
@return [Migrator] the new migrator Example
@return [Symbol] the name of the schema
Creates a new query for the schema.
@return [Query] the new query
Example
(name : Symbol, as as_name = nil, &)
Creates a new table in the schema.
@param name [Symbol] the name of the table
@param as_name [Symbol] the alias of the table
@yield [Table] the table being created
@return [Table] the created table Example
@return [Hash(Symbol, Table)] the tables in the schema
Creates a new update query for the schema.
@return [Update] the new update query Example
@return [String] the URI of the database
@return [String] the version of the schema
(call)
Reference
< Object
This module is part of the Cql namespace and is responsible for handling database alterations. This class represents an AlterTable object.
Example :
(table : Cql::Table, schema : Cql::Schema)
(name : Symbol, type : Any, as as_name : String | Nil = nil, null : Bool = true, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param type [Any] the data type of the column
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: true)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param size [Int32, nil] the size of the column (default: nil)
@param index [Bool] whether the column should be indexed (default: false)
Example Adding a new column with default options
Example Adding a new column with custom options
(name : Symbol, type : Any)
Changes the type of a column in the table.
@param name [Symbol] the name of the column to be changed
@param type [Any] the new data type for the column
Example Changing the type of a column
(name : Symbol, columns : Array(Symbol), unique : Bool = false)
Creates an index on the table.
@param name [Symbol] the name of the index
@param columns [Array(Symbol)] the columns to be indexed
@param unique [Bool] whether the index should be unique (default: false)
Example Creating an index
(column : Symbol)
Drops a column from the table.
@param column [Symbol] the name of the column to be dropped
Example Dropping a column
(name : Symbol)
Drops a foreign key from the table.
@param name [Symbol] the name of the foreign key to be dropped
Example Dropping a foreign key
(name : Symbol)
Drops an index from the table.
@param name [Symbol] the name of the index to be dropped
Example Dropping an index
(name : Symbol, columns : Array(Symbol), table : Symbol, references : Array(Symbol), on_delete : String = "NO ACTION", on_update : String = "NO ACTION")
Adds a foreign key to the table.
@param name [Symbol] the name of the foreign key
@param columns [Array(Symbol)] the columns in the current table
@param table [Symbol] the referenced table
@param references [Array(Symbol)] the columns in the referenced table
@param on_delete [String] the action on delete (default: "NO ACTION")
@param on_update [String] the action on update (default: "NO ACTION")
Example Adding a foreign key
(old_name : Symbol, new_name : Symbol)
Renames a column in the table.
@param old_name [Symbol] the current name of the column
@param new_name [Symbol] the new name for the column
Example Renaming a column
(new_name : Symbol)
Renames the table.
@param new_name [Symbol] the new name for the table
Example Renaming the table
(visitor : Expression::Visitor)
Converts the alter table actions to SQL.
@param visitor [Expression::Visitor] the visitor to generate SQL
@return [String] the generated SQL
Example Generating SQL for alter table actions
Cql::Column
< Cql::BaseColumn
< Reference
< Object
Primary key column definition
Example:
(name : Symbol = :id, type : PrimaryKeyType = Int64.class, as_name : String | Nil = nil, auto_increment : Bool = true, unique : Bool = true, default : DB::Any = nil)
:nodoc:
Reference
< Object
An index on a table This class represents an index on a table It provides methods for setting the columns and unique constraint It also provides methods for generating the index name
Example Creating a new index
(table : Table, columns : Array(Symbol), unique : Bool = false, name : String | Nil = nil)
Create a new index instance on a table
@param : table (Table) - The table to create the index on
@param : columns (Array(Symbol)) - The columns to index
@param : unique (Bool) - Whether the index should be unique (default: false)
@param : name (String, nil) - The name of the index (default: nil)
@return : Nil
@raise : Cql::Error if the table does not exist
@raise : Cql::Error if the column does not exist
Example
Generate the index name
@return : String
@raise : Nil
Example
(name : String | Nil)
Enum
< Comparable
< Value
< Object
Represents a database adapter module.
(type) : String
Returns the SQL type for the given type. @param type [Type] the type @return [String] the SQL type Example Getting the SQL type
Reference
< Object
Represents a table in the database. This class is responsible for handling table creation, modification, and deletion.
(table_name : Symbol, schema : Schema, as_name : String | Nil = nil)
(columns : Array(Symbol), unique : Bool = false, table : Table = self)
Adds a new column to the table.
@param columns [Array(Symbol)] the columns to be indexed
@param unique [Bool] whether the index should be unique (default: false)
@param table [Table] the table to add the index to (default: self)
@return [Index] the new index
Example Adding a new index
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, type : T.class, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false) forall T
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param type [T.class] the data type of the column
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param size [Int32, nil] the size of the column (default: nil)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
Creates the table in the database.
@return [Nil]
Example
Generates the SQL to create the table.
Example
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
Drops the table from the database.
@return [Nil]
Example
Generates the SQL to drop the table.
@return [String] the SQL query
Example
Gets table expression for Sql query generation
@return [Expression::Table] the table expression
Example
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol = :id, type : T.class = Int64, auto_increment : Bool = true, as as_name = nil, unique : Bool = true) forall T
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param type [Any] the data type of the column
@param auto_increment [Bool] whether the column should auto increment (default: true)
@param as_name [String, nil] an optional alias for the column
@param unique [Bool] whether the column should have a unique constraint (default: true)
Example Adding a new primary key column
(table_name : Symbol)
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, size : Int32 | Nil = nil, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
(name : Symbol, as as_name : String | Nil = nil, null : Bool = false, default : DB::Any = nil, unique : Bool = false, index : Bool = false)
Adds a new column to the table.
@param name [Symbol] the name of the column to be added
@param as_name [String, nil] an optional alias for the column
@param null [Bool] whether the column allows null values (default: false)
@param default [DB::Any, nil] the default value for the column (default: nil)
@param unique [Bool] whether the column should have a unique constraint (default: false)
@param index [Bool] whether the column should be indexed (default: false)
@return [Column] the new column
Example Adding a new column with default options
Adds a new column to the table.
Example Adding timestamps to the table
Truncates the table in the database.
@return [Nil]
Example
Generates the SQL to truncate the table.
@return [String] the SQL query
Example
(call)
Reference
< Object
Migrations are used to manage changes to the database schema over time. Each migration is a subclass of Migration
and must implement the up
and down
methods.
The up
method is used to apply the migration, while the down
method is used to rollback the migration. Migrations are executed in their version order defined. The Migrator
class is used to manage migrations and provides methods to apply, rollback, and redo migrations. The Migrator
class also provides methods to list applied and pending migrations.
Example Creating a new migration
Example Applying migrations
Example Rolling back migrations
Example Redoing migrations
Example Rolling back to a specific version
Example Applying to a specific version
Example Listing applied migrations
Example Listing pending migrations
Example Listing rolled back migrations
Example Listing the last migration
(schema : Schema)
Reference
< Object
The Migrator
class is used to manage migrations and provides methods to apply, rollback, and redo migrations. The Migrator
class also provides methods to list applied and pending migrations. Example Creating a new migrator
Example Applying migrations
(schema : Schema)
(migrations : Array(Migration.class))
Returns the applied migrations.
@return [Array(MigrationRecord)] Example Listing applied migrations
(steps : Int32 = Migrator.migrations.size)
Rolls back the last migration.
@param steps [Int32] the number of migrations to roll back (default: 1) Example Rolling back migrations
(version : Int64)
Rolls back to a specific migration version.
@param version [Int64] the version to roll back to Example Rolling back to a specific version
Returns the last migration. Example Listing the last migration
@return [Migration.class | Nil]
Returns the pending migrations.
@return [Array(MigrationRecord)] Example Listing pending migrations
Prints the applied migrations. Example Listing applied migrations
Prints the pending migrations. Example Listing pending migrations
(m : Array(Migration.class))
Prints the rolled back migrations.
@param m [Array(Migration.class)] the migrations to print
@return [Nil] Example Listing rolled back migrations
Redoes the last migration. Example Redoing migrations
(steps : Int32 = 1)
Rolls back the last migration.
@param steps [Int32] the number of migrations to roll back (default: 1) Example Rolling back migrations
(steps : Int32 = Migrator.migrations.size)
Applies the pending migrations.
@param steps [Int32] the number of migrations to apply (default: all) Example Applying migrations
(version : Int64)
Applies migrations up to a specific version.
@param version [Int64] the version to apply up to Example Applying to a specific version
Write documentation for Record module
Example Using the Record module
(attrs : Hash(Symbol, DB::Any))
Set the record's attributes from a hash
@param attrs [Hash(Symbol, DB::Any)] The attributes to set
@return [Nil]
Example Setting the record's attributes
Define instance-level methods for querying and manipulating data Fetch the record's ID or raise an error if it's nil
@return [PrimaryKey] The ID
Example Fetching the record's ID
Delete the record from the database
@return [Nil]
Example Deleting the record
Identity method for the record ID
@return [PrimaryKey] The ID
Example Fetching the record's ID
(id : Pk)
Set the record's ID
@param id [PrimaryKey] The ID
Example Setting the record's ID
Check if the record has been persisted to the database
@return [Bool] True if the record has an ID, false otherwise
Example Checking if the record is persisted
Define instance-level methods for querying and manipulating data Fetch the record's ID or raise an error if it's nil
@return [PrimaryKey] The ID
Example Fetching the record's ID
Define instance-level methods for saving and deleting records Save the record to the database or update it if it already exists
@return [Nil]
Example Saving the record
(fields : Hash(Symbol, DB::Any))
Delete the record from the database if it exists
@return [Nil]
Example Deleting the record
Update the record with the given record object
Example Updating the record
Update the record with the given fields
@param fields [Hash(Symbol, DB::Any)] The fields to update
@return [Nil]
Example Updating the record
DB::Mappable
< DB::Serializable
< Reference
< Object
Represents a migration record. @field id [Int64] the migration record id @field name [String] the migration name @field version [Int64] the migration version @field created_at [Time] the creation time @field updated_at [Time] the update time Example Creating a migration record
DB::Mappable
, DB::Serializable
(id : Int64, name : String, version : Int64, created_at : Time = Time.local, updated_at : Time = Time.local)
(rs : DB::ResultSet)
(rs : DB::ResultSet)
Define the has_many association module that will be included in the model to define a one-to-many relationship between two tables in the database and provide methods to manage the association between the two tables and query records in the associated table based on the foreign key value of the parent record.
param : name (Symbol) - The name of the association
param : type (Cql::Model) - The target model
param : foreign_key (Symbol) - The foreign key column in the target table
return : Nil
Example
(name, type, foreign_key, cascade = false)
(name, type, join_through, cascade = false)
Defines a many-to-many relationship between two models. This method will define a getter method that returns a ManyToMany::Collection. The collection can be used to add and remove records from the join table.
param : name (Symbol) - The name of the association
param : type (Cql::Model) - The target model
param : join_through (Cql::Model) - The join table model
param : cascade (Bool) - Delete associated records
Example
Reference
< Object
A collection of records for a one to many relationship This class is used to manage the relationship between two tables through a foreign key column in the target table and provide methods to manage the association between the two tables and query records in the associated table based on the foreign key value of the parent record.
param : Target (Cql::Model) - The target model
param : Pk (Int64) - The primary key type
return : Nil
Example
(key : Symbol, id : Pk, cascade : Bool = false, query : Cql::Query = (Cql::Query.new(Target.schema)).from(Target.table))
Initialize the many-to-many association collection class
param : key (Symbol) - The key for the parent record
param : id (Pk) - The id value for the parent record
param : target_key (Symbol) - The key for the associated record
param : cascade (Bool) - Delete associated records
param : query (Cql::Query) - Query object
return : ManyCollection
Example
(record : Target)
Create a new record and associate it with the parent record if it doesn't exist
param : record (Target)
return : Array(Target)
Example
Create a new record and associate it with the parent record
return : Array(Target)
Example
Clears all associated records from the parent record and the database
return : [] of T
Example
(record : Target)
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
(record : Target)
Delete the associated record from the parent record if it exists
param : record (Target)
return : Bool
Example
(id : Pk)
Delete the associated record from the parent record if it exists
param : id (Pk)
return : Bool
Example
Check if the association is empty or not
return : Bool
Example
Check if the association exists or not based on the attributes provided
param : attributes (Hash(Symbol, String | Int64))
return : Bool
Example
Find associated records based on the attributes provided for the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
Example
Returns a list if primary keys for the associated records
return : Array(Pk)
Example
(ids : Array(Pk))
Associates the parent record with the records that match the primary keys provided
param : ids (Array(Pk))
return : Array(Target)
Example
Reload the association records from the database and return them
return : Array(Target)
Example
Returns the number of associated records for the parent record
return : Int64
Example
(call)
Reference
< Object
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
(schema : Schema, table : Symbol)
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
Fetch all records of type T
@return [Array(T)] The records
Example Fetching all records
(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
Example Building a new user object
Count all records in the table
@return [Int64] The number of records
Example Counting all records
(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
(id : Pk)
Delete a record by ID
@param id [PrimaryKey] The ID of the record
Example Deleting a record by ID
Delete all records in the table
Example Deleting all records
Delete records matching specific fields
@param fields [Hash(Symbol, DB::Any)] The fields to match
Example Deleting records by email
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
(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
Example Fetching a record by ID
(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
Example Fetching a record by ID
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
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
Fetch the first record in the table
@return [T?] The first record, or nil if the table is empty
Example Fetching the first record
Fetch the last record in the table
@return [T?] The last record, or nil if the table is empty
Example Fetching the last record
(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
Example Paginating results
(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
Example Limiting results per page
(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
Example Updating a record by ID
(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
Example Updating a record by ID
(attrs : Hash(Symbol, DB::Any))
Update all records with given attributes
@param attrs [Hash(Symbol, DB::Any)] The attributes to update
Example Updating all records
(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
Example Updating records by email
Cql::Relations::Collection
< Reference
< Object
A collection of records for a many to many relationship This class is used to manage the relationship between two tables through a join table (through)
A many-to-many association occurs when multiple records of one model can be associated with multiple records of another model, and vice versa. Typically, it requires a join table (or a junction table) to store the relationships between the records of the two models.
Here’s how a many-to-many association is commonly implemented in CQL using Crystal.
Example
(key : Symbol, id : Pk, target_key : Symbol, cascade : Bool = false, query : Cql::Query = (Cql::Query.new(Target.schema)).from(Target.table))
Initialize the many-to-many association collection class
param : key (Symbol) - The key for the parent record
param : id (Pk) - The id value for the parent record
param : target_key (Symbol) - The key for the associated record
param : cascade (Bool) - Delete associated records
param : query (Cql::Query) - Query object
return : ManyCollection
Example
Clears all associated records from the parent record and the database
return : [] of T
Example
(record : Target)
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
(record : Target)
Delete the associated record from the parent record if it exists
param : record (Target)
return : Bool
Example
(id : Pk)
Delete the associated record from the parent record if it exists
param : id (Pk)
return : Bool
Example
(ids : Array(Int64))
Associates the parent record with the records that match the primary keys provided
param : ids (Array(Pk))
return : Array(Target)
Example