abstract class PgORM::Base

Included Modules

Extended Modules

Defined in:

pg-orm/base.cr

Constant Summary

AM_PARENT_TYPE = {:type => PgORM::Base} of Nil => Nil
Log = ::Log.for(self)
TABLES = [] of String

Constructors

Class Method Summary

Macro Summary

Instance Method Summary

Macros inherited from module PgORM::Validators

ensure_unique(field, scope = [] of Nil, callback = nil, &transform) ensure_unique

Macros inherited from module PgORM::Table

__process_table__ __process_table__, table(name) table

Instance methods inherited from module PgORM::Persistence

delete delete, destroy destroy, persisted? persisted?, reload! reload!, save(**options) save, save!(**options) save!, update(**attributes) update, update!(**attributes) update!, update_fields(**attributes) update_fields

Macros inherited from module PgORM::Associations

__process_assoc_serialization__ __process_assoc_serialization__, belongs_to(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil) belongs_to, has_many(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil, serialize = false) has_many, has_one(name, class_name = nil, foreign_key = nil, autosave = nil, dependent = nil) has_one

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

[View source]
def self.new(pull : JSON::PullParser) #

[View source]
def self.new(rs : DB::ResultSet) #

[View source]

Class Method Detail

def self.attributes : Array(Symbol) #

Returns all attribute keys.


def self.clear #

Removes all records from the table using DELETE.

This is slower than .truncate but respects foreign key constraints and triggers any database-level triggers.

Example

User.clear # DELETE FROM users

Warning: This deletes all data! Use with caution.


[View source]
def self.create(**attributes) #

Creates and attempts to save a new record to the database.

Returns the record regardless of whether it was saved successfully. Check #persisted? to confirm if the save succeeded.

Example

user = User.create(name: "John", email: "[email protected]")
if user.persisted?
  puts "User created with ID: #{user.id}"
else
  puts "Failed to create user: #{user.errors}"
end

See also: #save


[View source]
def self.create!(**attributes) #

Creates and saves a new record to the database.

Raises an exception if validation fails or the record cannot be saved.

Example

user = User.create!(name: "John", email: "[email protected]")
# => #<User id: 1, name: "John", email: "[email protected]">

# Raises PgORM::Error::RecordInvalid if validation fails
User.create!(name: "") # => Error!

See also: #save!


[View source]
def self.delete(ids : Enumerable(Value)) #

Deletes multiple records by an array of IDs.

Does not load records into memory or run callbacks. Use #destroy if you need callbacks.

Example

User.delete([1, 2, 3, 4, 5])

[View source]
def self.delete(ids : Enumerable(Enumerable(Value))) #

Deletes multiple records with composite primary keys.

Example

CompositeModel.delete([{key1: "a", key2: 1}, {key1: "b", key2: 2}])

[View source]
def self.delete(*ids) : Nil #

Deletes one or more records by ID.

Does not load records into memory or run callbacks. More efficient than destroy but doesn't trigger callbacks or update associations.

Example

# Delete single record
User.delete(1)

# Delete multiple records
User.delete(1, 2, 3, 4, 5)

[View source]
def self.from_rs(rs : DB::ResultSet) #

[View source]
def self.truncate(cascade = true) #

Quickly removes all records from the table using TRUNCATE.

Much faster than .clear for large tables, but:

  • Requires table-level locks
  • Resets auto-increment sequences
  • Can cascade to related tables if cascade: true

Example

# Truncate just this table
User.truncate(cascade: false)

# Truncate and cascade to related tables
User.truncate(cascade: true) # Also truncates related records

Warning: This deletes all data! Use with caution.


[View source]
def self.update(id : Value, args) : Nil #

Updates one or more records by ID without loading them into memory.

This is more efficient than loading, modifying, and saving records. Does not run validations or callbacks.

Example

# Update single record
User.update(1, {name: "John Updated"})
User.update(1, name: "John Updated")

# Update multiple records by ID
User.update([1, 2, 3], {active: false})

# Update with composite primary key
CompositeModel.update({key1: "a", key2: 1}, {status: "active"})

[View source]
def self.update(id : Enumerable(Value), args) : Nil #

Updates multiple records by an array of IDs.

Example

User.update([1, 2, 3], {active: false})

[View source]
def self.update(id : Enumerable(Enumerable(Value)), args) : Nil #

Updates multiple records with composite primary keys.

Example

CompositeModel.update([{key1: "a", key2: 1}, {key1: "b", key2: 2}], {status: "active"})

[View source]
def self.update(id, **args) : Nil #

Updates records using keyword arguments.

Example

User.update(1, name: "John", active: true)

[View source]

Macro Detail

macro __customize_orm__ #

[View source]
macro __nilability_validation__ #

[View source]
macro default_primary_key(name, auto_generated = true, converter = nil, **tags) #

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


[View source]
def apply_defaults #

Generate code to apply default values


def assign_attributes(params : HTTP::Params | Hash(String, String) | Tuple(String, String)) #

Assign to mulitple attributes via HTTP::Params.


def assign_attributes(model : PgORM::Base) #

Assign to multiple attributes from a model object


def assign_attributes #

Assign to multiple attributes.


def attributes #

Returns a Hash of all attribute values


def attributes_tuple #

Returns a NamedTuple of all attribute values.


def extra_attributes : Hash(String, PgORM::Value) #

[View source]
def persistent_attributes #

Returns a Hash of all attributes that can be persisted.


def to_json(json : JSON::Builder) #

[View source]