abstract struct Cql::Record(Pk)

Overview

Write documentation for Record module

Example Using the Record module

AcmeDB = Cql::Schema.define(:acme_db, adapter: Cql::Adapter::Postgres,
  uri: "postgresql://example:example@localhost:5432/example") do
  table :posts do
    primary :id, Int64, auto_increment: true
    text :title
    text :body
    timestamp :published_at
  end

  table :comments do
    primary
    bigint :post_id
    text :body
  end
end

struct Post < Cql::Record(Int64)
  db_context AcmeDB, :posts

  getter id : Int64?
  getter title : String
  getter body : String
  getter published_at : Time

  def initialize(@title : String, @body : String, @published_at : Time = Time.utc)
  end
end

struct Comment < Cql::Record(Int64)
  db_context AcmeDB, :comments

  getter id : Int64?
  getter post_id : Int64
  getter body : String

  def initialize(@post_id : Int64, @body : String)
  end
end

Defined in:

record.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def attributes(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

user.attributes = {name: "Alice", email: "[email protected]"}

[View source]
def 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

user.attributes
-> { id: 1, name: "Alice", email: " [email protected]" }

[View source]
def delete #

Delete the record from the database

  • @return [Nil]

Example Deleting the record

user.delete

[View source]
def id #

Identity method for the record ID

  • @return [PrimaryKey] The ID

Example Fetching the record's ID

user.id

[View source]
def id=(id : Pk) #

Set the record's ID

  • @param id [PrimaryKey] The ID

Example Setting the record's ID

user.id = 1

[View source]
def initialize #

[View source]
def persisted? #

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

user.persisted?

[View source]
def reload! #

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

user.reload!

[View source]
def save #

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

user.save

[View source]
def update(fields : Hash(Symbol, DB::Any)) #

Delete the record from the database if it exists

  • @return [Nil]

Example Deleting the record

user.delete

[View source]
def update #

Update the record with the given record object

Example Updating the record

bob = User.new(name: "Bob", email: " [email protected]")
id = bob.save

bob.reload!
bon.name = "Juan"

bob.update

[View source]
def update(**fields) #

Update the record with the given fields

  • @param fields [Hash(Symbol, DB::Any)] The fields to update
  • @return [Nil]

Example Updating the record

user.update(name: "Alice", email: " [email protected]")

[View source]